Skip to content

Instantly share code, notes, and snippets.

@rgl
Created January 1, 2014 22:08
Show Gist options
  • Save rgl/8212019 to your computer and use it in GitHub Desktop.
Save rgl/8212019 to your computer and use it in GitHub Desktop.
List the layers of a GIMP xcf file. This also exports the visible layers into a png file.
# This lists the layers of a GIMP xcf file. This also exports the visible layers into a png file.
#
# Execute the commands:
#
# curl -O https://bitbucket.org/rgl/make_dmg/downloads/background.xcf
# gimp -idf --batch-interpreter=python-fu-eval -b - -b 'pdb.gimp_quit(0)' < gimp_xcf_list_layers.py
#
# See http://www.gimp.org/docs/python/
# See http://gimpbook.com/scripting/
#
# -- Rui Lopes (ruilopes.com)
import re
from pipes import quote
path = "background.xcf"
png_path = path.replace(".xcf", ".png")
def flatten_layers(drawable, visible=True):
for layer in drawable.layers:
layer_visible = visible and layer.visible
yield (layer, layer_visible)
if not hasattr(layer, 'layers'):
continue
for l in flatten_layers(layer, layer_visible):
yield l
image = pdb.gimp_xcf_load(0, path, path)
layers = list(flatten_layers(image))
print 'IMAGE_HEADER width height'
print "IMAGE", image.width, image.height
print 'LAYER_HEADER visible x y width height name'
for layer, visible in layers:
print "LAYER", visible and "1" or "0", layer.offsets[0], layer.offsets[1], layer.width, layer.height, layer.name
pdb.gimp_image_undo_disable(image)
merged_layer = image.merge_visible_layers(CLIP_TO_IMAGE)
pdb.file_png_save2(
image,
merged_layer,
png_path,
png_path,
0, # interlacing (Adam7)
9, # compression level (0-9)
0, # save background color
0, # save gamma
0, # save layer offset
1, # save resolution
0, # save creation time
0, # save comment
1 # save color values from transparent pixels
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment