It is currently Thu Mar 28, 2024 4:19 pm


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Ways to execute a gimp python script from the command line
PostPosted: Tue Dec 06, 2016 5:03 pm  (#1) 
Offline
GimpChat Member

Joined: Nov 08, 2016
Posts: 45
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

#!/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()


Last edited by vA+6Q-Hv[BX] on Wed Dec 07, 2016 7:09 pm, edited 1 time in total.

Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: Ways to execute a gimp python script from the command line
PostPosted: Wed Dec 07, 2016 3:05 am  (#2) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4726
One of my script does:
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...

_________________
Image


Top
 Post subject: Re: Ways to execute a gimp python script from the command line
PostPosted: Wed Dec 07, 2016 8:06 pm  (#3) 
Offline
GimpChat Member

Joined: Nov 08, 2016
Posts: 45
ofnuts wrote:
One of my script does:
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")'

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


Top
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts How do you run GIMP from the command line?

1

No new posts Attachment(s) G'MIC> Pattern> Strip command line?

2

No new posts GEGL command line and layer modes?

2

No new posts Calling GIMP Plug-in From Command Line - on Windows

0

No new posts Attachment(s) Escape Line Script Ver-0.3

1



* Login  



Powered by phpBB3 © phpBB Group