How to run python scripts using gimpfu from windows command line?

扶醉桌前 提交于 2019-11-27 02:13:28

To run a python script you don't need to have it register as a plugin. IMHO you should even avoid this, this unnecessarily pollutes Gimp's menus and procedure name space. Here is an example:

The batch script (saved as batch.py):

#!/usr/bin/python
# -*- coding: iso-8859-15 -*-

import os,glob,sys,time
from gimpfu import *


def process(infile):
        print "Processing file %s " % infile
        image = pdb.file_jpeg_load(infile,infile)
        drawable = pdb.gimp_image_get_active_layer(image)

        print "File %s loaded OK" % infile
        pdb.plug_in_photocopy(image, drawable,8.,0.8,0.2,0.2)
        pdb.plug_in_cartoon(image, drawable, 7.,0.2)
        outfile=os.path.join('processed',os.path.basename(infile))
        outfile=os.path.join(os.path.dirname(infile),outfile)
        print "Saving to %s" % outfile
        pdb.file_jpeg_save(image, drawable, outfile, outfile, "0.5",0,1,0,"",0,1,0,0)
        print "Saved to %s" % outfile
        pdb.gimp_image_delete(image)


def run(directory):
        start=time.time()
        print "Running on directory \"%s\"" % directory
#   os.mkdir(os.path.join(directory,'processed'))
        for infile in glob.glob(os.path.join(directory, '*.jpg')):
                process(infile)
        end=time.time()
        print "Finished, total processing time: %.2f seconds" % (end-start)


if __name__ == "__main__":
        print "Running as __main__ with args: %s" % sys.argv

To call it:

gimp -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')" -b "pdb.gimp_quit(1)"

The parameters in slow-motion:

  • -idf: work without user interface, and load neither data nor fonts (you may perhaps need to keep the fonts to load pdfs)
  • --batch-interpreter python-fu-eval: whatever follows -b is Python, not script-fu
  • "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')": the code that we ask Gimp to execute, namely:
    • import sys;sys.path=['.']+sys.path;: extend the import path to include the current directory
    • import batch;: import the file with our script, which is now in a directory which is part of the path.
    • batch.run('./images'): call the run() function of the batch module we imported, giving it the name of a directory containing the images to process.
  • -b "pdb.gimp_quit(1)": another piece of python: exit when done.

Note how the command line cleverly uses double and single quotes to pass all parameters to Gimp and then to Python(*). And yes, you can use forward slashes as file separators in Windows.

To debug, matters are bit complicated in Windows because there is no always a stdout stream. Things that can help:

  • remove the -i parameter temporarily so that you get the UI and perhaps a chance to see messages.
  • Add --verbose which makes Gimp start a secondary console window.
  • There are other tricks to see messages listed here.
  • You can also start Gimp normally and run your script from the Python-fu console (Filters>Python-fu>Console). You will have to extend the path and import the file "manually".

(*) In a Linux/OSX shell one would do the opposite: single quotes for the shell, double quotes for Python.

Because the script you are using creates the image and then shows it in a window.... But you are calling Gimp with the --no-interface flag so the window is not shown.

IMHO to convert from PDF to JPEG, ImageMagick's convert command would be a lot simpler.

Also, in Windows Gimp comes with its own built-in Python 2.7 interpreter, so you have to write your Python code for that version, and there is no need to install another one.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!