BAM! Another reason I love Linux and free and open source software. 

My phone becomes an invaluable tool when I begin working on a project, namely for its camera. I have an elaborate system set up where I snap photos, they save to my phone as well as upload to my cloud when I’m connected to WiFi. Once my task is done for the project and the files have automatically found their way to my server, I sort and catalogue the files into specific project folders. Sometimes, these folders full of photos need to make their way to this website.

On my LG, the camera is spectacular, so I don’t hesitate to use it to its full potential. Each photo can be upwards of 10 MB though, which doesn’t make for fast web browsing, especially if one is simply browsing on their tablet or phone. If someone’s browsing on their desktop, chances are their monitor is running 1920 x 1080 resolution, so the native 5312 x 2988 resolution my LG produces actually serves against me.

I use GIMP for all my simple manipulation of photos and images. It’s easy to open up a file and scale the resolution… but it becomes very, very time consuming and mind-numbing when it comes to manipulating a hundred or so photos. And that’s where the little known Batch Mode that’s built into it helps me big time in taking care of what should be a simple task. I just want to take a bunch of photos and make them all 1080p!

Here’s how I went about doing it.

Firstly, I visited GIMP’s webpage which does a basic job of introducing the capabilities and where to find the information one needs to use it.

Then I perused their example of processing several files, admiring what looked to be some LISP functions and formatting I’m familiar with from my old AutoCAD LISP programming days.

I had no interest in applying an Unsharp Mask filter to the photos, as shown in the example, but I was interested in the “gimp-image-scale” procedure found in the Procedure Browser found under the Help menu. All I had to do was figure out how to use the parts of the example I was interested in, do away with what I don’t want, and insert what I do want.

To cut to the chase though, here’s the script that will need to be saved to your ~/.gimp-2.8/scripts directory (being mindful of the version number, of course):

(define (batch-resize-to-1080 pattern)
 (let* ((filelist (cadr (file-glob pattern 1))))
   (while (not (null? filelist))
          (let* ((filename (car filelist))
                 (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
                 (drawable (car (gimp-image-get-active-layer image))))
            (gimp-image-scale image 1920 1080)
            (gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
            (gimp-image-delete image))
           (set! filelist (cdr filelist)))))

Copy it, paste it into a text file, name it whatever you want. Just ensure it’s in GIMP’s script directory and it has the .scm file extension. Then in your terminal, navigate to the directory where you have copied your photos to manipulate (this script will overwrite your original files), and type out the following command:

gimp -i -b '(batch-resize-to-1080 "*.jpg")' -b '(gimp-quit 0)'

I’m not going to dig into how to interpret LISP (Lost In Silly Parenthesis), but I’ll try to dumb things down in a way that would allow the initiating scripter to take this example or GIMP’s example and understand what they can tweak and what they can’t tweak.

If it’s not immediately clear, it needs to be pointed out that all the 10 lines of code are called by that 1 line of code using “batch-resize-to-1080”. In the 10 lines of code, we’re essentially defining our own command that we can use within GIMP. So name this whatever you’d like, but just ensure it matches between the script and the command.

A big difference between this example and GIMP’s example, is the number of parameters/variables/arguments required to run the custom script. If you look back at GIMP’s example, you’ll see you have to type out the name of the custom script and then you have to specify a file extension, and a series of numbers. If it’s unclear why these need to be typed in, be sure to review “plug-in-unsharp-mask” in the Procedure Browser. These parameters are required to be entered on the command line so that this procedure can be used. But because I’m not interested in using this procedure, and I’m interested in using “gimp-image-scale” instead, I can leave out the “radius”, “amount”, and “threshold” parameters. 

The little difference between this example and GIMP’s example, is the one line in the rest of the script that gets replaced. Instead of:

(plug-in-unsharp-mask RUN-NONINTERACTIVE image drawable radius amount threshold)

I simply want:

(gimp-image-scale image 1920 1080)

Now, if you wanted the ability to define the pixel width and height at the command line, you’d want to add the variable names here on this line as well as back up in that first line of the script where you define the procedure name and its parameters. For my purposes though, I know this is the resolution I want and I likely won’t have to change it, and I like the idea of typing less to run the script.

And that’s essentially it; just know the example may look intimidating with all the parenthesis and indents and such, but when it comes down to making a script of your own, know what little you have to change.

In my case, it was less than I thought I’d actually have to change. I wanted to batch convert all the images to 1920 x 1080 resolution, but given the fact that some photos are taken in portrait and others in landscape, I thought I’d have to gather this information within each file and enumerate the width and height based on its orientation. I’m glad I tested early! GIMP actually manages this automatically and no further scripting effort was required on my part.

To sum it up… figure out the name of the procedure(s) you want to use automatically across multiple files, determine the parameters those procedures require using the Procedure Browser, identify the little differences I noted above and modify them to suit your needs.

If all’s good, you’ll see “batch command executed successfully” in the terminal.