103

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.

1
  • Parto please consider accepting the answer by @ThorSummoner askubuntu.com/a/749561/453746 as it seems to be a much better solution for new people finding this answer today without installing a plugin. Sep 28, 2019 at 4:23

11 Answers 11

140

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.

18
  • 13
    this was awesome, thanks- I think this is the best solution. Nothing to install and very fast. I just did it with GIMP with a PSD. Oct 11, 2016 at 13:11
  • 16
    Looks like ORA support was dropped in 2.10. :(
    – spacer GIF
    Aug 1, 2019 at 20:10
  • 2
    the only issue i have with this is that it doesn't use my layer names as file name, which means that i have to manually map them
    – Michael
    Aug 24, 2019 at 21:15
  • 5
    Not only does this still work great with 2.10.20, but there's even a stack.xml file in the archive that includes all of the layer parameters. A sed pipeline can make quick work of whipping up a set of commands to restore the original layer names: cat stack.xml |sed -e 's/><layer/\n/g;' |sed -E -e 's/.*name="([^"]*)".*src="([^"]*)".*/cp "\2" "\1"/;'
    – FeRD
    Jul 29, 2020 at 22:16
  • 2
    So, turns out the exporter for ORA files is written in Python. See my answer for why that matters.
    – FeRD
    Jul 30, 2020 at 0:06
59

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.

5
  • 2
    This was the easiest. Extract the file, copy the .py file to your ~/.gimp-2.8/plug-ins folder, make it executable, restart GIMP.
    – Parto
    Jun 20, 2014 at 12:27
  • Doesn't work in GIMP 2.6 btw.
    – installero
    Sep 6, 2014 at 10:44
  • 3
    This is also availabe at GitHub: github.com/khalim19/gimp-plugin-export-layers
    – moi
    Aug 24, 2016 at 8:30
  • the installation instructions for this plugin are stupid. :P Just install please, I don't care about directories.
    – ether_joe
    Jul 21, 2018 at 18:17
  • Agreed that the installed (for linux) is rough; this is the command I needed to use to make it work with an AppImage GIMP install: ./export_layers-3.2.1-linux.run --target /home/myname/.config/GIMP-AppImage/2.10/plug-ins/ -- -g /path/to/gimp-git-2.10.5-20180719.glibc2.15-x86_64.AppImage -i /home/myname/.config/GIMP-AppImage/2.10/plug-ins/
    – user272901
    Aug 21, 2018 at 5:13
12

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
4
  • If you only wanted specific layers, then this is the better way to go... Jul 10, 2019 at 18:20
  • 8
    Also, if you only wanted two or three layers. Not so great if you find yourself (as I did yesterday) wanting to export each and every one of the 79 frames in an animated gif.
    – FeRD
    Jul 31, 2020 at 21:25
  • 2
    This doesn't answer to the question "I want to export every single layer as an individual image (PNG format preferably) automatically" as this method is not automated
    – brunetton
    Mar 20, 2022 at 14:55
  • Adding to the previous comment, if you still wanted to go like this, there's an easier way than copy & pasting layers: hide all layers except the one you want to save it, unhide next layer & save it and keep repeating.
    – Christian
    Nov 12, 2023 at 16:31
10

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.

2
  • 8
    Note: GIFs only support 256 colour palettes, so it would not be possible to do 24-bit PNGs this way. Mar 24, 2016 at 5:34
  • 2
    Furthermore, for large images convert takes ages while unziping an ORA file is almost instant. Good idea, though. Dec 16, 2018 at 21:27
7

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

2
  • Works very nice! Thanks! Jan 4, 2021 at 18:14
  • 1
    Looks like for i in {0..42}; do magick layeredImage.xcf["${i}"] "${i}.png"; done appears to work in Bash 5.1.16. Mar 21, 2023 at 10:49
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.
2
6

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)
2
  • 1
    Hilarious, thanks! On windows, I had to add a target path like this, because otherwise it tries to write to c:\windows\system32: savefn(img, lay, 'c:\\temp\\'+outname, outname)
    – ernstkl
    Mar 18, 2021 at 15:34
  • 1
    @ernstkl Ah, yeah, that probably makes sense. I'll incorporate a full-path example as well, since launching graphically is surely way more common the running gimp from a terminal like I did. (You can actually write that path without doubling slashes using raw strings (r"C:\temp\"), or as "C:/temp/"+outname since Python has supported separator translation on Windows for a while now. The "most correct" canonical form would probably be os.path.join("C:", r"\temp", outname) but that's pretty ugly. And kind of pointless when the path is entered directly, rather than stored in a variable.)
    – FeRD
    Mar 19, 2021 at 20:30
3

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
1
  • 1
    Nice one, the only downside is that transparency is not preserved. If you dont need transparency, this is a nice and simply way to go about it
    – devklick
    Jun 6, 2023 at 20:05
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.)

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.

1
  • This worked well for me, but there are issues with transparency. I did not need it in my project but I don't think it will work if you need to preserve the layer transparency/alpha mask.
    – pipe
    Mar 10, 2022 at 1:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .