Writing a GIMP python script

前端 未结 1 1195
长发绾君心
长发绾君心 2020-12-28 09:34

What I want to do is to open gimp from a python program (with subprocess.Popen, perhaps), and in the same time, gimp will start with a python script that will open an image

相关标签:
1条回答
  • 2020-12-28 09:38

    Ok I finally got it working. I used the GIMP Python scripting to create a gimp plugin which can do a tonne of stuff including the layers you mentioned. Then you can just run gimp from the command line passing arguments to the python gimp script. The article Using Python-Fu in Gimp Batch Mode was an excellent resource for learning how to call gimp plugins from the command line. The example below will load the specified image into gimp, flip it horizontally, save and exit gimp.

    flip.py is the gimp plug-in and should be placed in your plug-ins directory which was ~/.gimp-2.6/plug-ins/flip.py in my case.

    flip.py

    from gimpfu import pdb, main, register, PF_STRING
    from gimpenums import ORIENTATION_HORIZONTAL
    
    def flip(file):
        image = pdb.gimp_file_load(file, file)
        drawable = pdb.gimp_image_get_active_layer(image)
        pdb.gimp_image_flip(image, ORIENTATION_HORIZONTAL)
        pdb.gimp_file_save(image, drawable, file, file)
        pdb.gimp_image_delete(image)
    
    args = [(PF_STRING, 'file', 'GlobPattern', '*.*')]
    register('python-flip', '', '', '', '', '', '', '', args, [], flip)
    
    main()
    

    from the terminal one could run this:

    gimp -i -b '(python-flip RUN-NONINTERACTIVE "/tmp/test.jpg")' -b '(gimp-quit 0)'
    

    or from Windows cmd:

    gimp-console.exe -i -b "(python-flip RUN-NONINTERACTIVE """<test.jpg>""")" -b "(gimp-quit 0)"
    

    or you can run the same from a python script using:

    from subprocess import check_output
    cmd = '(python-flip RUN-NONINTERACTIVE "/tmp/test.jpg")'
    output = check_output(['/usr/bin/gimp', '-i', '-b', cmd, '-b', '(gimp-quit 0)'])
    print output
    

    I tested both to make sure they work. You should see the image get flipped after each script run.

    0 讨论(0)
提交回复
热议问题