Skip to content

Instantly share code, notes, and snippets.

@1bardesign
Created April 30, 2023 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1bardesign/99343824201ebea123e34862c4a81e11 to your computer and use it in GitHub Desktop.
Save 1bardesign/99343824201ebea123e34862c4a81e11 to your computer and use it in GitHub Desktop.
script-fu to export all image layers from a gimp file to png
; export all (image) layers as separate pngs
; available in the layer menu
;
; Max Cahill 2023
; Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;
; recursive layer scan routine that pulls all layers out of the image into a list
; from https://gitlab.gnome.org/GNOME/gimp/-/issues/6761
; modified to not return group layers at all
(define (layer-scan img rootFolder)
(let*
(
(getChildren 0)
(layerList 0)
(i 0)
(layer 0)
(allLayerList ())
)
(if (= rootFolder 0)
(set! getChildren (gimp-image-get-layers img))
(if (equal? (car (gimp-item-is-group rootFolder)) 1)
(set! getChildren (gimp-item-get-children rootFolder))
(set! getChildren (list 1 (list->vector (list rootFolder))))
)
)
(set! layerList (cadr getChildren))
(while (< i (car getChildren))
(set! layer (vector-ref layerList i))
(cond
(
(equal? (car (gimp-item-is-group layer)) 1)
(set! allLayerList (append allLayerList (layer-scan img layer)))
)(
else
(set! allLayerList (append allLayerList (list layer)))
)
)
(set! i (+ i 1))
)
allLayerList
)
)
; save a layer into a reasonably-named file
(define (save-layer img layer)
(let*
(
; figure out the base of the parent file name
(parent-filename (car (gimp-image-get-filename img)))
(parent-base (substring parent-filename 0 (- (string-length parent-filename) 4))) ;assumed xcf
; this layer's name
(name (car (gimp-layer-get-name layer)))
; the combined filename
(filename (string-append parent-base "_" name ".png"))
)
(file-png-save-defaults
; dont' ask
RUN-NONINTERACTIVE
; use the image boundary etc
img
; pass the layer as a drawable so it's the only thing saved
layer
; use the generated filename
filename
"" ;unused?
)
)
)
; the actual entry point
(define (script-fu-export-layers) (
let*
(
(img 1) ;is there anything else?
)
(map (lambda (layer)
(save-layer 1 layer)
) (layer-scan 1 0))
#t
))
; registration
(script-fu-register
;function name
"script-fu-export-layers"
;menu label
"Export Layers"
;description
"Export all the current image's layers as separate images."
;author
"Max Cahill"
;copyright notice
"Copyright 2023, Max Cahill;"
;date created
"April 26, 2023"
;image type that the script works on
""
;a string variable (unused for now since we'll sort out customising the format if we need it)
;SF-STRING "Image Format" "png"
)
(script-fu-menu-register "script-fu-export-layers" "<Image>/Layer")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment