The 3 examples below are based on:
http://stackoverflow.com/questions/5794 ... mmand-lineI've always gone to imagemagick when doing batch, but was wondering if I could do everything with gimp.
I was trying to answer the following questions:
- How can you easily execute a gimp
python script from the command line?
- Does the script have to be registered?
* Do all registered scripts appear in a gimp menu?
- How do you pass in parameters?
I came up with 3 ways below.
Are there any other ways to execute gimp
python scripts?
At this time (newbie to gimp scripts and
python,) I like Option 1 the best because I don't have to remember a long command or register the script in gimp. Parameters are easily passed on the command line.
Option 2 doesn't register the script, but I could not figure out how to pass a single file to the script (sample.xcf).
Option 3 registers the script. Parameters are passed via the parameter section of the register function.
Execute 1: ./save-xcf-to-png-01.sh sample.xcf
#!/bin/bash
declare -r xcfFile="${1:?Missing xcf Input File}"
gimp -idf --batch-interpreter=python-fu-eval -b - << EOF
import gimpfu
def convert(filename):
img = pdb.gimp_file_load(filename, filename)
new_name = filename.rsplit(".",1)[0] + ".png"
layer = pdb.gimp_image_merge_visible_layers(img, 1)
pdb.gimp_file_save(img, layer, new_name, new_name)
pdb.gimp_image_delete(img)
convert('${xcfFile}')
pdb.gimp_quit(1)
EOF
Execute 2: gimp -idf --batch-interpreter=
python-fu-eval -b - < save-xcf-to-png-02.py
# Note required here, see: /usr/lib/gimp/2.0/plug-ins/python-eval.py
# from gimpfu import *
from glob import glob
def convert(filename):
print "Filename: " + filename
img = pdb.gimp_file_load(filename, filename)
new_name = filename.rsplit(".",1)[0] + ".png"
layer = pdb.gimp_image_merge_visible_layers(img, 1)
pdb.gimp_file_save(img, layer, new_name, new_name)
pdb.gimp_image_delete(img)
for filename in glob("*.xcf"):
convert(filename)
pdb.gimp_quit(1)
Execute 3: gimp -idf -b '(
python-fu-save-xcf-files-to-png RUN-NONINTERACTIVE "sample.xcf")' -b '(gimp-quit 0)'
from gimpfu import *
# from glob import glob
def convert_images(filename):
print "Filename: " + filename
img = pdb.gimp_file_load(filename, filename)
new_name = filename.rsplit(".",1)[0] + ".png"
layer = pdb.gimp_image_merge_visible_layers(img, 1)
pdb.gimp_file_save(img, layer, new_name, new_name)
pdb.gimp_image_delete(img)
register(
"save_xcf_files_to_png",
"Save xcf files to png",
"Save xcf files to png",
"test",
"test",
"2016",
"<Toolbox>/MyScripts/Examples/Ex12: Save xcf files to png...",
"",
[
(PF_STRING, "filename", "_String:", None)
],
[],
convert_images
)
main()