113

I have come to a situation where I have an image in Gimp with multiple layers. Now, I want to export every single layer as an individual image (PNG format preferably) automatically to a folder somewhere.

Is this possible?

The long method: Hide all layers except one, crop the section you want, export image. Hide saved layer, unhide another one, crop section, export. Repeat. Kinda cumbersome for an image with about 20 layers.

karel
  • 122,292
  • 133
  • 301
  • 332
Parto
  • 15,647

11 Answers11

150

If PNG is an acceptable output format, one option is to export it as Open Raster (.ora), an open specification for layered-image files.

  1. Export Image as Open Raster (.ora)

    File -> Export As ...

    myfile.ora

  2. Open myfile.ora as an archive, with a program like file-roller or 7zip.

    On Ubuntu:

    $ file-roller myfile.ora
    
    $ # note, later version of file-roller on ubuntu hard-code file suffixes, 
      # and refuse to open ".ora" files, work around by renaming the file to ".zip"
    $ ln -s myfile.ora myfile.ora.zip
    $ file-roller myfile.ora.zip
    

    alternatively

    $ unzip myfile.ora
    

    All your layers will be png images under /data, Extract them and use at will.

ThorSummoner
  • 3,372
60

You may try also this plugin, Export Layers. I've tested it with png format and it worked. You just select the folder and the format and you get all the layers there, each one in its own file.

MrMesees
  • 251
Emil
  • 991
14

First of all you don't need any plugin. Even you don't need to crop anything. Few simple steps.

  1. Select a layer. To do that just click on that particular layer.
  2. Copy the layer to a clipboard using Ctrl+C
  3. Now create new image using Ctrl+Shift+V

That's it. Now you can simply export that layer to any format.

  1. Use Shift+Ctrl+E
abu_bua
  • 11,313
Pritalgo
  • 307
11

One could export the image as an animated GIF. This will save each layer as a separate frame in the GIF.

Then, the ImageMagick command convert -coalesce ./myfile.gif outfile%05d.png will extract the frames as PNG images.

MRule
  • 454
9

The best thing about ThorSummoner's answer is that it called attention to the OpenRaster export plugin, which as it turns out lives in the file file-openraster.py in the GIMP installation.

By reading its code (and with some assistance from the built-in procedure browser), I was able to determine that the layers of a GIMP XCF can be saved to individual PNGs by going to Filters > Python-fu > Console in the interface, and entering the following into the built-in Python interpreter:

import os

If you have multiple images open, you may need to adjust

img = gimp.image_list()[0]

savefn = gimp.pdb['file-png-save-defaults'] outpath = "/home/$USER/Pictures" # (or r"C:\Users$USER\Pictures", etc)

for lay in img.layers: # Even if your layer names contain spaces, not a problem outname = lay.name + ".png" savefn(img, lay, os.path.join(outpath, outname), outname)

# type an extra newline to exit the indented block

You'll see the progress meter in the image window's status bar start whipping through all of your layers, writing each one to a PNG file of the same name, in whatever directory you specified as outpath. (Which must already exist, otherwise add an os.makedirs(outpath, exist_ok=True) before the loop.)

If any of your layer names are the same, that is a problem, because this will happily overwrite any previously-written files. Caveat GIMPtor.

Edit: If you do have same-named layers, you could easily ignore the names and instead write out the layers in numbered files. Just replace the final loop above with something like:

for n, lay in enumerate(img.layers):
    outname = f"Layer {n:03}.png"
    savefn(img, lay, os.path.join(outpath, outname), outname)

That'll write the layers into PNG files named "Layer 000.png" through "Layer 999.png" (or however many layers are present, if fewer than 1000).

If the Gimp's version of Python doesn't support f-strings (Python 3.6+), this is exactly equivalent:

outname = "Layer {0:03}.png".format(n)
FeRD
  • 283
  • 3
  • 9
8

I think you can try to find something with ImageMagick : apt-get install imagemagick. It seems to be able to handle XCF format and you can export a layer in png using a [N] in the command, where N is the level of your layer.

Source : http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=17603

ImageMagick Read Mods : http://www.imagemagick.org/Usage/files/#read_mods

Bahaïka
  • 935
6

Certainly, this work for the plugin Export Layers to File.

Features:

  • Manipulate the layers in layer group.
  • Export texts, patterns and layer filters.
  • Prefix name for the image files has to be given.
  • Export only in jpg, png, bmp formats but the required formats can be added easily.
swift
  • 3,291
4

Many years later, GIMP has removed the functionality from the accepted solution and some of the other code solutions have succumbed to code rot (I'm unable to run the GIMP exporter with Python3).

As this is one of the first hits from Google when asking how to export layers from GIMP to their own PNG files, I feel duty bound to provide an updated answer.

I'm using GIMP 2.10.18.

Here is one solution that worked for me:

  • Export the XCF file as a PDF (File -> Export as) with the following in mind:
    • Make sure to select Layers as pages (bottom layers first)
    • Make sure to de-select Apply layer masks before saving
    • Make sure to de-select Convert bitmaps to vector graphics where possible
    • Make sure to de-select Omit hidden layers and layers with zero opacity
  • Use the command line tool pdftoppm to export as PNG files.

For example, if you were to export the XCF file as the file exported_layers.pdf, the following will create each layer as it's own PNG image with the prefix out_ (e.g. out_01.png, out_01.png etc.):

pdftoppm -png -r 300 exported_layers.pdf out_

The -r option is the resolution and presumably this should match whatever default GIMP is set when exporting to PDF files. I found 300 kept the resolution in the layers of my original XCF file but your mileage may vary.

On Ubuntu, on can install pdftoppm by issuing:

apt-get install poppler-utils
abetusk
  • 141
2

As an alternative approach to my previous script-based suggestion, I have finally discovered that you can also do this with one of the tools available if you have the GIMP GAP installed. (That's the Gimp Animation... Package? Plugin? Something like that.)

It's a bit hidden in the forest of GAP menu options, but the "Video" menu selection "Split Image to Frames..." will do so by saving each layer as an individual file. In whatever format you specify (via the file extension), and with several options for how to handle the layers. Here's the dialog that came up when I chose that option after saving an un-optimized animated GIF as a 52-layer /tmp/testfile.xcf:

Split Image into Frames dialog window

Despite the filename preview, the frames were saved as /tmp/testfile_01.png through /tmp/testfile_52.png, as specified. (It doesn't update the displayed string in response to your selections, but it does honor them.)

FeRD
  • 283
  • 3
  • 9
1

I was doing the same thing and trying to download the plugins mentioned in the answers. As the Gimp website is currently down, I could not get the plugins and I had to find another solution.

What I ended up doing was use the screenshot software Shutter, which allows you to select a region of the screen and then you can repeat the same screenshot of that region with just 1 click. So it becomes a 2-click per layer operation: hide layer, screenshot, hide next layer, screenshot, ...

Much faster than anything else I could come up with, and takes less than a minute for 20 layers. You may lose image quality although in my case it was not a problem.

0

Export all frames from Gimp as MNG, then use ImageMagick for separating them to PNGs.

convert frames.mng frame.png

ImageMagick extends each file name with numbers (if you don't format the string) like "frame-1.png" and so on.