Windows: Batch processing images with GIMP and saving them to new files

4

On my windows machine, I want to use GIMP to modify all PNG files (filename.png) in the current directory and to save the output file to the sub-directory out/filename.png. The processing that is needed is:

  • adding rounded corners
  • adding drop shadow
  • resizing the image

For that, I tried to use the following script and placed it under C:\Program files\GIMP\share\gimp\2.0\scripts:

(define (script-fu-shadowcorners infile outfile width height)
  (let* ((image (car (file-png-load 1 infile infile)))
         (drawable (car (gimp-image-active-drawable image)))
        )
    (script-fu-round-corners image drawable 10 FALSE 8 8 30 FALSE FALSE)
    (script-fu-drop-shadow image drawable 8 8 12 '(0 0 0) 45 TRUE)
    (gimp-image-scale image width height)
    (gimp-file-save RUN-NONINTERACTIVE image drawable outfile outfile)
  )
)

(script-fu-register "script-fu-shadowcorners" "" "" "" "" "" "RGB*" SF-FILENAME "Infile" "infile.png" SF-FILENAME "Outfile" "outfile.png")

Afterwards, I tried to call this script with the following batch file:

for %%x in (*.png) do "C:\Program files\GIMP\bin\gimp-2.8" -i -b "(script-fu-shadowcorners \"%%x\" \"newfolder\\%%x\")"

It did not work. It does always say it could not find the files, it's very slow, and there is no output.

Now that the CMD command works, at least, the problem is not completely solved. In CMD, there appears a line after my batch command that replaces the %%x wildcard by the first file name - but only the first. In the output directory, I can not find files other than the first one, either. Nevertheless, Gimp's output is batch command executed successfully.

What am I doing wrong? Can you help me? Thank you very much!

caw

Posted 2013-01-15T17:39:40.410

Reputation: 141

1Does the command work with a single PNG specified at the command line? Ensure paths with spaces (such as to the gimp-2.8 dir) are quoted. Also, try adding an echo before "C:\Program files... in the for loop and see what's printed out. Are the paths to the PNGs ok? – Karan – 2013-01-16T02:59:44.160

Thank you! It turned out that the correct command would have been this one-liner: for %%x in (*.png) do "C:\Program files\GIMP\bin\gimp-2.8" -i -b "(script-fu-functionname \"%%x\" \"newfolder\\%%x\")" – caw – 2013-01-16T18:08:31.353

Unfortunately, this does only process 1 of the 24 .png files in my directory. Can anybody see why? – caw – 2013-01-16T18:09:10.400

Any errors causing the script to abort? I'm actually getting confused by that script-fu bit - do you really need to escape the quotes with backslashes for it? – Karan – 2013-01-16T18:10:38.513

Yes, that's correct and necessary, as the whole script-fu parameter is wrapped by quotes as well, so you have to escape the inner quotes. This is what made it working. Since script-fu starts and one PNG is modified, we can be sure that this syntax is correct. The question is what lets script-fu cancel its job?! – caw – 2013-01-16T18:30:49.207

1If you run it at the command-line (replace all %% with single %), after closing Gimp when it's done with the 1st PNG, does it print the command for the 2nd PNG? – Karan – 2013-01-16T18:33:55.807

Strangely, it processes all files if I use the same command (except for the double %) in the command line. Why doesn't it work when it's in the batch file? – caw – 2013-01-16T18:44:42.607

When I add -b "(gimp-quit 0)" to the batch file content, it processes all files as well. However, it always requires me to "press any key" before proceeding to the next image. – caw – 2013-01-16T18:47:47.697

Probably because at the cmd line it runs one command then returns, whereas in the batch file it runs and waits for you to quit the launched program i.e. Gimp before it can continue with the loop. – Karan – 2013-01-16T18:52:19.697

No, in CMD it did even run without the gimp-quit command at the end. Is there any way to make that work in the batch file as well or to skip the "press any key" prompts when using gimp-quit? – caw – 2013-01-16T18:55:42.130

Do you have a pause anywhere in the batch file? If so, remove it and see. – Karan – 2013-01-16T18:59:07.033

No, removed that already before. – caw – 2013-01-16T19:02:14.220

Can you edit the question and copy-paste the contents of the batch file? – Karan – 2013-01-16T23:02:53.170

Did that already, you can find the updated contents in the question above. – caw – 2013-01-16T23:46:12.020

Well the official tutorials all have gimp-quit included, but I would have thought -i for no interface would imply the ability to run silently without requiring user input. No idea why Gimp is pausing.

– Karan – 2013-01-16T23:50:00.483

Thank you, Karan! Finally found the problem, please see my answer below. – caw – 2013-01-17T01:37:18.353

Answers

5

The solution had two parts:

Part 1: The syntax of the batch command was wrong, especially the quotes need to be set correctly:

for %%x in (*.png) do "C:\Program files\GIMP\bin\gimp-2.8" -i -b "(script-fu-functionname \"%%x\"

Parts 2: Gimp always waiting for me to "press any key" was due to the wrong binary used: Instead of gimp-2.8, gimp-console-2.8 must be used.

Thanks, Karan, for hinting at the solutions.

caw

Posted 2013-01-15T17:39:40.410

Reputation: 141