Skip to content

Instantly share code, notes, and snippets.

@phantamanta44
Created March 8, 2018 04:04
Show Gist options
  • Save phantamanta44/da2b52aa402c1841e03038ebc0d5008c to your computer and use it in GitHub Desktop.
Save phantamanta44/da2b52aa402c1841e03038ebc0d5008c to your computer and use it in GitHub Desktop.
(define (batch-autocrop 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)))
(width (car (gimp-image-width image)))
(height (car (gimp-image-height image)))
(layer (car (gimp-layer-new image width height INDEXEDA-IMAGE "temp" 100 NORMAL-MODE))))
(gimp-image-insert-layer image layer 0 0)
(let* ((layers (gimp-image-get-layers image))
(layer-count (car layers))
(layer-array (cadr layers))
(i (- layer-count 1))
(clones ()))
(while (> i 0)
(let* ((elem (aref layer-array i))
(clone (car (gimp-layer-copy elem TRUE))))
(set! clones (cons clone clones)))
(set! i (- i 1)))
(while (not (null? clones))
(gimp-image-insert-layer image (car clones) 0 1)
(set! clones (cdr clones)))
(set! i (- layer-count 1))
(while (> i 0)
(gimp-image-merge-down image layer EXPAND-AS-NECESSARY)
(set! layer (aref (cadr (gimp-image-get-layers image)) 0))
(set! i (- i 1))))
(plug-in-autocrop RUN-NONINTERACTIVE image layer)
(gimp-image-remove-layer image layer)
(file-gif-save RUN-NONINTERACTIVE image (car (gimp-image-get-active-layer image)) filename filename 0 1 1 0)
(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