2014-10-11

GIMP script (Scheme) to scale multiple files at once (batch).

I made this back in January of this year, but I forgot to post it. I searched around briefly, but at the time, I found nothing that I could just drop in and use. So I had to write this.

It is written in Scheme. On my MacBook, I drop this script in ~/Library/Application Support/GIMP/2.8/scripts. Then from the GIMP menu bar select Filters | Script-Fu | Refresh Scripts. This script should then show up under the GIMP menu bar selection Image | Transform | Batch Scale By Factor.

There are a lot of reasons we would need to perform this a batch scale of images files, but mine was related to iOS development. Retina images must be twice the resolution of non-Retina images in order to look good. So I used Retina images as originals and then I had to scale everything to half that size for non-Retina.

The script does not overwrite the original files. It makes new files with "_.jpg" appended to the file name, even if the file name already ends in ".jpg". Which reminds me, this script saves all files as JPEGs, even if the originals are not. If you want different output, I'm sure you can figure out how to change it.

Hey, even though I wrote this to not overwrite the originals, I highly recommend that you never operate on originals. Make a copy of your originals in a temp directory and run the batch process on the copies.

(define (script-fu-scale-by-factor fileglob factor)
  (let*
    (
      (files (cadr (file-glob fileglob 0)))
    )
    (do-scale-to files factor)
  )
)

(define (do-scale-to files factor)
  (while (not (null? files))
    (let*
      (
        (file (car files))
        (image (car (gimp-file-load 1 file file)))
        (width (car(gimp-image-width image)))
        (height (car(gimp-image-height image)))
        (newfile (string-append file "_.jpg"))
        (drawable (car (gimp-image-get-active-layer image)))
      )
      (gimp-image-scale image (* width factor) (* height factor))
      (gimp-file-save 1 image drawable newfile newfile)
    )
    (set! files (cdr files))
  )
)

(script-fu-register
  "script-fu-scale-by-factor"               ;func name
  "Batch Scale By Factor"                   ;menu label
  "Scale multiple images by given factor."  ;description
  "Jeffery Martin"                          ;author
  "Copyright 2014\
   Fluffy White Puppy Software"             ;copyright notice
  "2014-01-06"                              ;date created
  ""                                        ;image type that the script works on
  SF-STRING "fileglob" "/tmp/photos/image-*.jpg"
  SF-VALUE "factor" "0.5"
)

(script-fu-menu-register "script-fu-scale-by-factor" "<Image>/Image/Transform")

Here is what it looks like in operation. Original images files:



Here is the dialog when you run the script.



And the directory after the script run.



And notice that the image files are half the size because I input a scaling factor of 0.5 in the script dialog.


2 comments:

  1. A better solution:
    Use Scale with mogrify:

    mogrify -scale 5000% *png

    While at the directory (Folder) with all the copies of your pics.
    Scale is good for Pixel-Art. So it won't get blurred as a biffer image.

    ReplyDelete
    Replies
    1. Oh! You are talking about ImageMagick (http://imagemagick.org) I did not know about that product. Thank you.

      Delete