Export all open images on GIMP

Currently I use GIMP for my day-to-day image editing. Nothing fancy, mind you (I’m no graphics designer)

However, recently I had the need to export several images at one time, which is a tedious task.

Searching online, I found this post that had a macro in Script-Fu (a LISP dialect) that did what I wanted.

Yes, it exported all open images as IMAGE00000.ext (where ext is the image-type extension).

Fast forward a few days and I needed it again. But, this time I wanted to keep the file name but exported as a different file format... And the script didn't do what I wanted.

So, I rolled up my sleeves and went to work.

After a few false starts -- LISP is not my forte, but oh well -- I finally got to a point I liked it.

I kept the basic functionality and added mine as an option that the user select it with a checkbox.

Ah, yes! Befor I forget, the script should be saved in the following folder: %UserProfile%\AppData\Roaming\GIMP\2.10\scripts

Here it is:

; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.

(define (drop-extension filename)
  (unbreakupstr (reverse (cdr (reverse (strbreakup filename ".")))) ".")
)

(define (pad-number number total-width)
  (let* ((number-string (number->string number))
         (num-digits (string-length number-string))
         (num-zeroes (- total-width num-digits))
         (zeroes-string (make-string num-zeroes #\0)))
    (string-append zeroes-string number-string)))
    
(define (script-fu-save-all-images 
		  inSaveType
          inDir 
          inUseImageFileName
		  inFileName 
		  inFileNumber)
  (let* (
          (i (car (gimp-image-list)))
          (ii (car (gimp-image-list))) 
          (image)
          (newFileName "")
          (saveString "")
          (curImageName "")
          (pathchar (if (equal? (substring gimp-dir 0 1) "/") "/" "\\"))
        )
    (set! saveString
      (cond 
        (( equal? inSaveType 0 ) ".jpg" )
        (( equal? inSaveType 1 ) ".bmp" )
        (( equal? inSaveType 2 ) ".png" )
        (( equal? inSaveType 3 ) ".tif" )
      )
    ) 
    (while (> i 0) 
      (set! image (vector-ref (cadr (gimp-image-list)) (- i 1)))
      (set! curImageName (car (gimp-image-get-filename image)))
      (set! newFileName (string-append 
                         inDir 
                         pathchar 
                         inFileName 
                         (pad-number ( + inFileNumber ( - ii i ) ) 5)
                         saveString
                        )
      )
      (if (and (equal? inUseImageFileName 1) (not (equal? curImageName "")))
        (set! newFileName (
                string-append 
                (drop-extension curImageName)
                saveString)
        )
      )
      (gimp-file-save RUN-NONINTERACTIVE 
                      image
                      (car (gimp-image-get-active-layer image))
                      newFileName
                      newFileName
      ) 
      (gimp-image-clean-all image) 
      (set! i (- i 1))
    )
  )
) 

(script-fu-register 
 "script-fu-save-all-images" 
 "<Image>/Tools/Save ALL As" 
 "Save all opened images as ..." 
 "Paulo Santos" 
 "Copyright © 2023 - Paulo Santos" 
 "2014/04/21" 
 ""
 SF-OPTION     "Save File Type" (list "jpg" "bmp" "png" "tif")
 SF-DIRNAME    "Save Directory" ""
 SF-TOGGLE     "Save in the same folder as the image file\n(ignore 'Save Directory')" 0
 SF-STRING     "Save File Base Name" "IMAGE"
 SF-ADJUSTMENT "Save File Start Number" (list 0 0 9000 1 100 0 SF-SPINNER)
 )


Comments