In gimp : how to save the different layers of a design in separate files/images?


Question

I wonder if there's a way in gimp to make the different layers of a design into separate image files without having to save the image each time. I hope this is clear. I use layers also as variations on the theme and for my latest projects i d like to put all those variations individually in a new context.

1
5
3/28/2011 4:47:00 AM

The link to the script in the post pointed by Pankaj was broken, so I wrote my own "save layers" script in Python. To use the script, save the following script to your plug-in directory (e.g. ~/.gimp-x.x/plug-ins/save_all_layers.py), and if on Linux/Mac give the script the execute permission bit (e.g. chmod +x ~/.gimp-x.x/plug-ins/save_all_layers.py), then restart GIMP.

#!/usr/bin/env python

from gimpfu import *
from os.path import join

def save_all_layers(image, __unused_drawable, directory, name_pattern):
    for layer in image.layers:
        filename = join(directory, name_pattern % layer.name)
        raw_filename = name_pattern % layer.name
        pdb.gimp_file_save(image, layer, filename, raw_filename)

register(
    "python_fu_save_all_layers",
    "Save all layers into separate files",
    "Save all layers into separate files",
    "Lie Ryan",
    "Lie Ryan",
    "2012",
    "<Image>/File/Save layers as images...",
    "*",
    [
        (PF_DIRNAME, "directory", "Directory to put the images on", "/"),
        (PF_STRING, "name_pattern", "Pattern for file name, %s will be replaced with layer name", "%s.png"),
    ],
    [],
    save_all_layers
)

main()

Now you can just go to File > Save layers as images to, well, save layers as images.

Alternatively, if you do not fancy restarting GIMP or if you do not want to install the plugin permanently, then copy-paste the following function into the Python-fu console:

def save_all_layers(image, directory, name_pattern):
    for layer in image.layers:
        filename = join(directory, name_pattern % layer.name)
        raw_filename = name_pattern % layer.name
        pdb.gimp_file_save(image, layer, filename, raw_filename)

and use it as such:

>>> # first, find the image object
>>> gimp.image_list()
[<gimp.Image 'Untitled'>, <gimp.Image 'slice_5.xcf'>]
>>> # I want to edit slice_5.xcf, so I select that image
>>> img = gimp.image_list()[1]
>>> # double check that you got the correct image
>>> img
<gimp.Image 'slice_5.xcf'>
>>> # and finally call the function, the %s will be replaced by layer names
>>> save_all_layers(img, "/path/to/save/directory/", "image_%s.png")
7
4/19/2012 4:44:00 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
^