5

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
  • i guess you have to get a plugin for saving layers in separate file
    – Jack
    Mar 28, 2011 at 5:20

3 Answers 3

7

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")
2
  • works perfectly well, thanks! exports EVERYTHING, including hidden layers and transparency. UPVOTED! :)
    – tony gil
    May 3, 2013 at 16:06
  • Works like magic, one catch though, you should name the file exactly as he said 'save_all_layers.py' or it wont work Jul 25, 2013 at 19:31
2

Just as a small update to the great @Lie Ryan's answer. There can be problems with layers which names contain special characters (like '/'), so save_all_layers function could be adjusted to handle this (based on this SO answer).

from os.path import join
import string

valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)

def make_safe(s):
    return ''.join(c for c in s if c in valid_chars)

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

wherever i know you have to install a script for doing the same :

the link is given below check this and read this carefully..

http://registry.gimp.org/node/15617

@ all i am a newbie to gimp so might be less knowledge :(

if i am wrong than please correct me

Thanks

1
  • Within that page is indeed a link to a script which is intended to save multiple layers separately.
    – mgkrebbs
    Mar 30, 2011 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.