René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Script-fu example 09: Resizing images

The following function resizes an image:
resize_image.scm
(define (resize-image filename-in filename-out new-width new-height)
  (let* ((image    (car (gimp-file-load RUN-NONINTERACTIVE filename-in "")))
         (drawable (car (gimp-image-active-drawable image)))
        )

     (gimp-image-scale image new-width new-height)
     (gimp-file-save   RUN-NONINTERACTIVE image drawable filename-out "")
  )
)
c:\> start gimp-2.2.exe -d -i -b  "(resize-image \"c:\\temp\\\\ex_08.jpg\" \"c:\\\\temp\\\\ex_08_r.jpg\" 500 500)" "(gimp-quit 0)"
Now, resizing all jpg's in a directory:
file_basename.scm
(define (file-basename filename)
  (let*
    (
      (broken-up (strbreakup filename "."))
      (wo-last-r (cdr (reverse broken-up)))
      (wo-last   (reverse wo-last-r))
      (result "")
    )
    (while wo-last
      (set! result (string-append result (car wo-last) ))
      (set! wo-last (cdr wo-last))
      (if (> (length wo-last) 0) (set! result (string-append result ".")))
    )
    result
  )
)
ex_09.scm
(define (ex_09 file-pattern new-width new-height )

  (let* ( (filelist (cadr (file-glob file-pattern 1))))

    (while (not (null? filelist))
      (let* ( (cur-file  (car filelist)) )
      
        (resize-image 
           cur-file 
           (string-append (file-basename cur-file) "_resized.jpg")
           100 
           100
        )

        (set! filelist (cdr filelist))
      )
    )
  )
)
c:\> start gimp-2.2.exe -d -i -b  "(ex_09 \"c:\\temp\\ex_*.jpg\" 100 100)" "(gimp-quit 0)"
See also other examples