Export Gimp XCF Layers as Jpegs

November 30, 2011

I use The Gimp a lot for my web and image work, so much so that I find it easier and more logical than photoshop.

Most of the designers I work with use Photoshop, so I can’t escape it, but I generally avoid Photoshop whenever possible.

Often, I use gimp to make slides, using a single xcf file + layers to create the slideshow, then saving them out individually. This can be a little time consuming, especially for larger slideshows or files with a bunch of layers.

So, I went in search for a plugin to export layers to jpegs in Gimp. I did not find anything that was exactly what I needed. I did, however, find something that was designed for PNG files, so I made a few changes and it seems to work fine.

It is possible I did not look hard enough, but the following python script works well and is quite simple.

Exporting Individual Layers in Gimp to Jpegs

The following python script was first posted here by Kevin Payne. Thanks for the productivity boost!!!

I only made a few small changes, mainly to enable jpg support, remove spaces from names, and changing the menu location.

You may also want to check out the plugin to export Visible Layers as JPG

Description

Exports the layers of a XCF file as individual jpg images.

The name of the layer is used as the filename, except spaces are replaced with “_”. The files are saved to the same directory as the XCF image.

Option is added under “File->Save->Export as JPGS

Installation

In linux save to ~/.gimp-26/plug-ins/SaveAsJpeg.py

Make sure to chmod +x the file

Note that the gimp folder name might be different.

If you need more help: http://en.wikibooks.org/wiki/GIMP/Installing_Plugins

Plugin Code


#!/usr/bin/env python
# Author: Kevin Payne, small modification by John
# Copyright 2011 Kevin Payne
# License: GPL v3
# GIMP plugin to export the layers of an .xcf as jpg images
# each jpg file has the name of the layer
# For PhotoComix:- http://gimpchat.com/viewtopic.php?f=9&t=985#p10807

from gimpfu import *
import os.path
import pygtk
import gtk

# something to help with debugging
def debugMessage(Message):
dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, Message)
dialog.run()
dialog.hide()

# unique_filename from http://stackoverflow.com/questions/183480/is-this-the-best-way-to-get-unique-version-of-filename-w-python
def unique_filename(file_name):
counter = 1
file_name_parts = os.path.splitext(file_name) # returns ('/path/file', '.ext')
while os.path.isfile(file_name):
file_name = file_name_parts[0] + '_' + str(counter) + file_name_parts[1]
counter += 1
return file_name

# this is the bit that does all the work
def export_layers_as_jpg(img):
layer_ids = img.layers # get the layers in the image
directory_name = os.path.dirname(img.filename) # where is the source image

# debugMessage(directory_name)

num_layers = len(layer_ids)
for layer_num in range (0, num_layers): # work through layers (change to "num_layers-1" if you don't want the bottom layer)
layer_name = pdb.gimp_drawable_get_name(layer_ids[layer_num])
# debugMessage(layer_name)

# replace any illegal characters to be on the safe side
layer_name = layer_name.replace(":", ";")
layer_name = layer_name.replace("/", ";")
layer_name = layer_name.replace("\\", ";")
layer_name = layer_name.replace("?", ";")
layer_name = layer_name.replace("*", ";")
layer_name = layer_name.replace("\"", ";")
layer_name = layer_name.replace(" ", "_")

# debugMessage(layer_name)

# build the new file path - puts the saved layers in the same place as the source image
jpg_file = os.path.join(directory_name, layer_name + ".jpg")
jpg_file = unique_filename(jpg_file)

# debugMessage(png_file)
pdb.file_jpeg_save(img, layer_ids[layer_num], jpg_file, jpg_file, 1, 1, 1, 1, 0, 1, 1, 1, 1)

# The End of the main routine

# menu registration
register(
"python-fu-export-layers-as-jpg",
"Export Layers as JPGS",
"Export Layers as JPGS",
"paynekj",
"GimpChat",
"12.01.2011",
"Export as JPGS",
"*",
[
(PF_IMAGE, "image", "Input image", None),
],
[],
export_layers_as_jpg,
menu="/File/Save"
)

main()

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 44 other followers