Switch to full style
Post all Gimp scripts and script writing questions here
Post a reply

Ways to execute a gimp python script from the command line

Tue Dec 06, 2016 5:03 pm

The 3 examples below are based on:
http://stackoverflow.com/questions/5794 ... mmand-line

I'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

Code:
#!/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
Code:
# 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)'

Code:
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()

Re: Ways to execute a gimp python script from the command line

Wed Dec 07, 2016 3:05 am

One of my script does:
Code:
gimp-2.6 -idf --batch-interpreter python-fu-eval -b 'import sys; sys.path=["."]+sys.path;import batch;batch.run("./images")' -b 'pdb.gimp_quit(1)'


So the parameter "./images" is passed as part of the code to run...

Re: Ways to execute a gimp python script from the command line

Wed Dec 07, 2016 8:06 pm

ofnuts wrote:One of my script does:
Code:
gimp-2.6 -idf --batch-interpreter python-fu-eval -b 'import sys; sys.path=["."]+sys.path;import batch;batch.run("./images")' -b 'pdb.gimp_quit(1)'


So the parameter "./images" is passed as part of the code to run...


Ahhh, good one. I removed the "pdb.gimp_quit(1)" from the command line and added it to the script.

Execute 4: gimp -idf --batch-interpreter=python-fu-eval -b 'import sys; sys.path=["."]+sys.path;import save2png;save2png.convert("sample.xcf")'

Code:
#!/usr/bin/python
from gimpfu import *

def convert(filename):
  print "Filename: " + filename + " converting to png"
  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)

  pdb.gimp_quit(1)
Post a reply