Siyao's profile火星猫PhotosBlogLists Tools Help

Blog


    August 15

    用Python给Gimp写插件

    原由:好不容易从emule上拖了一本“LaTeX Companion”下来,结果发现排版实在是感觉不爽。下载下来的电子档是扫描版的,但是扫描者似乎为了节省页面,将对开的两页书扫描到一个页面上,所以pdf文档的一个页面就有左右两面书,看起来很不方便,于是想办法把这些页面裁开。不知道有什么pdf工具能完成这样的任务,所以我采用的是比较麻烦的办法。

    Linux下暂时没有找到提取pdf文件的工具,所以我先在虚拟机下运行Acrobat Professional,将每一个页面保存为一个jpg文件。这样,下面的任务就是将每一个文件一左一右对半裁成两个文件,一共有280多个这样的页面,几乎是不可能手工完成的,就算可以,也不愿意。原来处理数码照片的时候比较满意一个windows软件“光影魔术手”中的批处理功能,可是遗憾的是这个软件不能按照我需要的方式批量裁减这些文件,所以要另想办法。

    回到Linux下,研究传说中Photoshop的替代者Gimp,具有比较合适的裁减功能,但遗憾的是不具有批处理的功能。在菜单里翻来翻去,发现一个叫python-fu的东东,貌似Gimp提供给python的接口。查了相关资料,这是Gimp提供给Python语言写插件的一个接口,功能很全面,而且非常简洁,写插件非常方便。在http://www.gimp.org/docs/python/index.html 有用Python写GImp插件的文档。用下面这些代码就完成了我的需要:

    #!/usr/bin/env python

    from gimpfu import *
    import os

    def python_batch_clip( xorg = 0, yorg = 0, width = 400, height = 300,
                                    directory = "/home",
                                    savedirectory = "/home",
                                    filename_postfix = ""):
        for entry in os.listdir(directory):
            if directory[len(directory)-1] == "/":
                fullpath = directory + entry
            else:
                fullpath = directory + "/" + entry

            if fullpath[-4:len(fullpath)] == ".jpg":
                # we assume this is a jpg file
                print "Processing file ", fullpath
                image = pdb.file_jpeg_load(fullpath, fullpath)
                pdb.gimp_image_crop(image, width, height, xorg, yorg)
                if savedirectory[len(savedirectory)-1] == "/":
                    savepath = savedirectory + entry[:-4]
                else:
                    savepath = savedirectory + "/" + entry[:-4]
                if filename_postfix != "":
                    savepath = savepath + "_" + filename_postfix
                savepath = savepath + ".jpg"
                print "Saving file ", savepath
                pdb.file_jpeg_save(
                                        image,
                                        pdb.gimp_image_get_active_drawable(image),
                                        savepath, savepath, 1, 0, 1, 0, "", 0, 0, 0, 0)


    register(
            "python_fu_batch_clip",
            "Crop the jpg files in a directory",
            "Crop the jpg files in a directory",
            "Zheng Siyao",
            "Zheng Siyao",
            "2006",
            "<Toolbox>/Python-Fu/zsy/batch_clip",
            "RGB*, GRAY*",
            [
                (PF_INT, "x_origin", "X origin", 0),
                (PF_INT, "y_origin", "Y origin", 0),
                (PF_INT, "width", "Width", 400),
                (PF_INT, "height", "Height", 300),
                (PF_FILE, "dir_path", "Directory", "/home"),
                (PF_FILE, "save_path", "Save to", "/home"),
                (PF_STRING, "postfix", "Filename Postfix", "")
            ],
            [],
            python_batch_clip)

    main()

    python_batch_clip()这个函数遍历目录下所有.jpg结尾的文件,读取每一个文件到image对象中,对每个image对象进行裁减操作,最后将image对象写入文件。

    register函数有固定的格式,通过这个函数向Gimp注册插件,最后的main运行插件。Gimp启动的时候会在插件目录下寻找可用的插件,因此在使用之前必须给这个文件添加可执行属性。这个插件不是对一个文件的滤镜,属于一种工具,因此将其规为<Toolbox>类,这样,启动Gimp后,不打开任何文件,就能使用这个插件了。

    运行插件的时候如下图所示,可以看出,python-fu还提供了一个输入参数的图形界面,同时,插件运行的进度也反应在这个界面上。当然,也可以用print向终端输出自己需要的信息。

    Comments (3)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Colin Wuwrote:
    学以致用啊,不错不错。学习ing...
    打算什么时候也学学Python,下次重装系统时体验一下Ubuntu。
    Sept. 27
    豌豆wrote:
    呵呵,你很少到我这里坐客的,谢谢你给我的鼓励,我还好只是还不太适应,不管结果怎样我都尽力而为。弟弟!你是好样的。
    Sept. 16
    昕 付wrote:
    真的是见识了~越来越佩服你了~~!!程序高手!!!
    Aug. 16

    Trackbacks (1)

    The trackback URL for this entry is:
    http://zsycat.spaces.live.com/blog/cns!44293220A213592D!471.trak
    Weblogs that reference this entry