Below is a "basic" Script-Fu that takes two layer "subsets" and does the "cross-product" of them and exports a PNG for each combination. We have used this to create large numbers of such "button" images.
For example:
- Background-1
- Background-2
- Background-3
- Foreground-1
- Foreground-2
Factor 1 = "Background" and Factor 2 = "Foreground" Will create 6 exported PNGs.
Also note that you can have additional visible layers, e.g. drop shadow, and these will get copied through to each exported PNG. Just make sure you set the visibility of layers how you want and save before running the script.
This is as-is unsupported. If you don't know about Scheme or programming, you are probably better off doing the manual labor. If you are interested, or just love S-expressions, this script outputs some diagnostics and has some comments, so if you turn on the Script Console, you can see some output.
Copy the text below to "export-pngs.scm" and put that file into your GIMP "User" scripts folder, on Windows 7, "/Users/yourusername/.gimp-2.6/scripts". When you restart GIMP, there will be a new command File|Create|Create PNGs.
Please please please (that's 3) maintain attribution!
(define (script-fu-export-pngs filename outputfolder basename factor1 factor2)
(let*
(
(image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
(layerinfo (gimp-image-get-layers image))
(layerarray (cadr layerinfo))
(layers (vector->list layerarray))
(layers-f1 (matching-layers layers factor1))
(layers-f2 (matching-layers layers factor2))
)
(gimp-message (string-append "layers-f1=" (layer-list layers-f1)))
(gimp-message (string-append "layers-f2=" (layer-list layers-f2)))
(gimp-image-undo-disable image)
(layers-visible (append layers-f1 layers-f2) FALSE)
(for-each
(lambda (layer-f1)
(for-each
(lambda (layer-f2)
(let*
(
(output-filename (string-append outputfolder "/" basename "_"
(car (gimp-drawable-get-name layer-f1)) "_"
(car (gimp-drawable-get-name layer-f2)) ".png"
))
)
(output-png image layers (list layer-f1 layer-f2) output-filename)
)
)
layers-f2
)
)
layers-f1
)
(gimp-image-undo-enable image)
(gimp-displays-flush)
)
)
; set visibility of all layers in the list
; layers: layer list.
; vis: TRUE or FALSE.
(define (layers-visible layers vis)
(for-each
(lambda (layer)
(gimp-drawable-set-visible layer vis)
)
layers
)
)
; top-level save-as-PNG
; image: source image.
; layers: layer list.
; layer-factors: current-factors list.
; output-filename: full path to save.
(define (output-png image layers layer-factors output-filename)
(let*
(
(width (car (gimp-image-width image)))
(height (car (gimp-image-height image)))
(type (car (gimp-image-base-type image)))
(output (car (gimp-image-new width height type)))
)
(gimp-message (string-append "output-filename=" output-filename "\noutput-image=" (layer-list layer-factors)))
(gimp-image-undo-disable output)
(output-image
layers
layer-factors
(lambda (layer)
(let*
(
(new-layer (car (gimp-layer-new-from-drawable layer output)))
)
;(gimp-message (string-append "add-layer " (car (gimp-drawable-get-name new-layer))))
(gimp-drawable-set-visible new-layer TRUE)
(gimp-image-add-layer output new-layer 0)
(gimp-image-lower-layer-to-bottom output new-layer)
)
)
)
(file-png-save2
RUN-NONINTERACTIVE output
(car (gimp-image-merge-visible-layers output CLIP-TO-IMAGE))
output-filename output-filename
FALSE 9 FALSE FALSE FALSE FALSE FALSE FALSE TRUE
)
(gimp-image-delete output)
)
)
; traverse layers and call add-layer as necessary
; layers: layer list.
; layer-factors: current-factors list.
; add-layer: function to add selected layers.
(define (output-image layers layer-factors add-layer)
(let*
()
(for-each
(lambda (layer)
(let*
(
(layer-visible (car (gimp-drawable-get-visible layer)))
)
(cond
((contains? layer layer-factors) (add-layer layer))
((= layer-visible TRUE) (add-layer layer))
)
)
)
layers
)
)
)
; scan layer list for layer-names starting with the prefix
; layers: layer list.
; prefix: starting prefix of layer name.
(define (matching-layers layers prefix)
(let*
(
(output '())
)
(for-each
(lambda (layer)
(let*
(
(layer-name (car (gimp-drawable-get-name layer)))
)
(if (match-prefix? layer-name prefix) (set! output (append output (list layer))))
)
)
layers
)
output
)
)
; return #t/#f if target starts with the prefix
; target: target string to match.
; prefix: starting prefix.
(define (match-prefix? target prefix)
(let*
(
(numchars (min (string-length prefix) (string-length target)))
(pfx (substring target 0 numchars))
)
(string-ci=? pfx prefix)
)
)
; return #t/#f if item exists in the list
; layer: target layer.
; layers: layer list.
(define (contains? layer layers)
(let*
((retv #f))
(map
(lambda (lx)
(if (eqv? lx layer) (set! retv #t))
)
layers
)
retv
)
)
; create comma-delimited list of layer names
; list: layer list.
(define (layer-list list)
(let*
((buf ""))
(for-each
(lambda (lx)
(set! buf (string-append (car (gimp-drawable-get-name lx)) "," buf))
)
list
)
(if (> (string-length buf) 0)
(set! buf (substring buf 0 (- (string-length buf) 1)))
)
buf
)
)
(script-fu-register
"script-fu-export-pngs"
"Create PNGs"
"Create PNGs from 2 layer subsets"
"escape-llc"
"CC 3 Attribution"
"2009-11-13"
""
SF-FILENAME "Image Filename" ""
SF-DIRNAME "Output Folder" ""
SF-STRING "Base Name" ""
SF-STRING "Factor 1" "color"
SF-STRING "Factor 2" "glyph"
)
(script-fu-menu-register "script-fu-export-pngs" "<Image>/File/Create")