![Offline Offline](./styles/GimpChat/imageset/en/icon_user_offline.gif) |
Script Coder |
![User avatar](./download/file.php?avatar=218_1278261758.jpg) |
Joined: Jun 22, 2010 Posts: 1171 Location: Here and there
|
Try this: #!/usr/bin/env python # Author: Kevin Payne # Copyright 2011 Kevin Payne # License: GPL v3 # GIMP plugin to export the layers of an .xcf as png images # each png file has the name of the layer # For PhotoComix:- http://gimpchat.com/viewtopic.php?f=9&t=985 # The saving layer name in a file was requested here: # http://gimpchat.com/viewtopic.php?f=9&t=978&sid=36599f7fc54901155c684afdfc03a5c2&p=10910#p10872
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
def format_layer_name_for_saving(layer_name): #[G'MIC] Graphic Novel FX : -gimp_photocomix_booost2 1,4.36364,10.4727,5,20,0.12,14,0,0.5,0.54,2.90909,9,0.936364,1,0.5,0.54,0.78 # into {USER_INPUT_HERE}{Graphic Novel FX}{gimp_photocomix_booost2}{gimp_photocomix_booost2}{1}{4.36364}{10.4727}{5}{20}{0.12}{14}{0}{0.54}{2.9}{9}{0}{0.93}{1}{0.5}{0.54}{0.78} # Throw away the [G'MIC] new_layer_details = layer_name.replace("[G'MIC] ", "") temp = new_layer_details.find(":") label = new_layer_details[0:temp-1] new_layer_details = new_layer_details[temp+2:]
# extract the function name temp = new_layer_details.find(" ") function = new_layer_details[0:temp-1] function = function.lstrip("-") new_layer_details = new_layer_details[temp+1:]
# replace the commas with curly brackets new_layer_details =new_layer_details.replace("," , "}{") return "{" + label + "}{" + function + "}{" + function + "}{" + new_layer_details + "}" def photocomix_safe_file_name(file_name): # replace any illegal characters to be on the safe side file_name = file_name.replace(":", ";") file_name = file_name.replace("/", ";") file_name = file_name.replace("\\", ";") file_name = file_name.replace("?", ";") file_name = file_name.replace("*", ";") file_name = file_name.replace("\"", ";") return file_name
# this is the bit that saves the layers def export_layers_as_png(img): layer_ids = img.layers # get the layers in the image directory_name = os.path.dirname(img.filename) # where is the source image gimp.progress_init() 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]) layer_name = photocomix_safe_file_name(layer_name)
# build the new file path - puts the saved layers in the same place as the source image png_file = os.path.join(directory_name, layer_name + ".png") png_file = unique_filename(png_file)
pdb.file_png_save2(img, layer_ids[layer_num], png_file, png_file, 1, 9, 1, 1, 0, 1, 1, 1, 1) gimp.progress_update(layer_num/num_layers*100) # The End of the main routine
# menu registration register( "python-fu-export-layers-as-png", "Export Layers as PNGs", "Export Layers as PNGs", "paynekj", "GimpChat", "12.01.2011", "Export Layers as separate PNGs", "*", [ (PF_IMAGE, "image", "Input image", None), ], [], export_layers_as_png, menu="<Image>/contributed/" )
# This one is to format the layer names and save them in a text files def photocomix_save_layer_names(img, layer_descriptions): layer_ids = img.layers # get the layers in the image directory_name = os.path.dirname(img.filename) # where is the source image layer_description_list = layer_descriptions.split(",") # make a list out of the user input string num_layers = len(layer_ids) # how many layers are there in total text_file_name = "" # place to store the name of the text file text_file_contents = "" # place to store the contents of the text file description_num = 0 # index for the user supplied list for layer_num in range (0, num_layers): # work through layers layer_name = pdb.gimp_drawable_get_name(layer_ids[layer_num]) if (layer_name[0] == "["): # only work on layers that have a name starting with [ this_layer_description = layer_description_list[description_num] # get the user supplied description for this layer description_num += 1 formatted_line = "{" + this_layer_description + "}" + format_layer_name_for_saving(layer_name) # build the line to be written in the file text_file_contents = text_file_contents + formatted_line + "\r\n" # if this is the first layer to be saved then get the user description to use as the file name if (len(text_file_name) == 0): parts = formatted_line.split("{") text_file_name = parts[1] text_file_name = text_file_name.rstrip("}") + "_preset.txt" text_file_name = photocomix_safe_file_name(text_file_name) # make sure the filename is safe to use textFile = open(os.path.join(directory_name,text_file_name) , "w") textFile.write(text_file_contents) textFile.close debugMessage(text_file_name + " Saved")
# register( "python-fu-photocomix-save-layer-names", "Save Layer Names", "Save Layer Names", "paynekj", "GimpChat", "13.01.2011", "Save layer names in a file", "*", [ (PF_IMAGE, "image", "Input image", None), (PF_STRING, "layer_descriptions", "Layer Descriptions:", "Layer1,Layer2"), ], [], photocomix_save_layer_names, menu="<Image>/contributed/" )
main()
I've put both parts in the one file because they naturally go together. So this one replaces the other. Again they will both appear in the contributed menu, for you to move where you actually want them. I have made the dialog ask for your user input as a comma separated list, but I'm not happy with this as it's going to be too easy to get the number of inputs different to the number of layer names to save, so in the longer term I'll have a look at prompting for the individual layers - which is going to get complicated. (unless some python expert is reading and wants to offer a solution)
_________________ Kevin
|
|