Skip to content

Instantly share code, notes, and snippets.

@inferiorhumanorgans
Created September 20, 2013 09:33
Show Gist options
  • Save inferiorhumanorgans/6635205 to your computer and use it in GitHub Desktop.
Save inferiorhumanorgans/6635205 to your computer and use it in GitHub Desktop.
Based on this script: http://www.gimptalk.com/index.php?/topic/2347-command-line-script-fu-to-scale-an-image/ This'll do non-destructive resizing.
;; Routine scales images based on the two parameters
;; if x-factor > 0 then image width is scaled to that width (if y-factor==0 then keep aspect ratio)
;; if x-factor < 0 then image width is scaled by magnitude of that amount (y-factor==0 means flip horizontally)
;; if y-factor > 0 then image height is scaled to that height (if x-factor==0 then keep aspect ratio)
;; if y-factor < 0 then image height is scaled by magnitude of that amount (x-factor==0 means flip vertically)
;; if x-factor AND y-factor = 0 then rotate image 90 deg CCW
(define (batch-scale-image pattern x-factor y-factor suffix)
(let* (
(filelist (cadr (file-glob pattern 1)))
(width)
(height)
(flip-horiz FALSE)
(flip-vert FALSE)
(extension "png")
(basename)
(fullname)
)
(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)))
)
(cond
( (< x-factor 0)
(set! width (* (car (gimp-image-width image)) (abs x-factor)))
)
( (> x-factor 0)
(set! width x-factor)
)
( (= x-factor 0)
(cond
( (> y-factor 0)
(set! width (* (/ y-factor (car (gimp-image-height image))) (car (gimp-image-width image))))
)
( (= y-factor 0)
(set! flip-horiz TRUE)
(set! width (car (gimp-image-width image)))
)
( (< y-factor 0)
(set! flip-horiz TRUE)
(set! width (* (abs y-factor) (car (gimp-image-width image))))
)
)
)
)
(cond
( (> y-factor 0)
(set! height y-factor)
)
( (< y-factor 0)
(set! height (* (car (gimp-image-height image)) (abs y-factor)))
)
( (= y-factor 0)
(if (<= x-factor 0)
(set! flip-vert TRUE)
)
(set! height (* (/ width (car (gimp-image-width image))) (car (gimp-image-height image))))
)
)
(gimp-image-scale-full image width height 3)
(if (and (= flip-horiz TRUE) (= flip-vert TRUE))
(gimp-image-rotate image ROTATE-270)
(begin
(if (= flip-horiz TRUE)
(gimp-image-flip image ORIENTATION-HORIZONTAL)
)
(if (= flip-vert TRUE)
(gimp-image-flip image ORIENTATION-VERTICAL)
)
)
)
(set! basename (car (strbreakup filename ".")))
(set! fullname (string-append basename "." suffix "." extension))
(gimp-file-save RUN-NONINTERACTIVE image drawable fullname fullname)
(gimp-image-delete image))
(set! filelist (cdr filelist))
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment