Exporting Visible Layers as JPGs in Gimp

December 10, 2011

After playing around a bit with the export layers to jpg plugin from last week, I have used it quite a bit and it is very helpful.

For a little info on installing or more background, you can check out that post. Basically, it slightly tweaks a plugin I found to export layers in Gimp to individual jpg files.

After some use, I found there were some times when I only want to export visible layers from an XCF and not ALL of the layers. This was especially true after making changes to one slide, while leaving the other the same.

This is basically the same code as last week, except it adds a check to see if the layer is visible or not. It adds the below if statement.

if pdb.gimp_layer_get_visible(layer_ids[layer_num]) == 0 :

I still have both installed right now, but I may only use the export visible layers one. In any case, it has been fun to do some playing with Python, because I am not very familiar with it, but know it is very powerful.

The Updated Code for SaveAsJPGVisible.py

#!/usr/bin/env python
# Author: Kevin Payne, small modification by John
# Copyright 2011 Kevin Payne
# License: GPL v3
# GIMP plugin to export the Visible 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)

# 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)

if pdb.gimp_layer_get_visible(layer_ids[layer_num]) == 0 :

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
"Export Visible Layers as JPGS",
"Export Visible Layers as JPGS",
"Export Visible Layers as JPGS",
(PF_IMAGE, "image", "Input image", None),



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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s