From c884a97be64ad64a1f35d71f0dbc7ed2c8d761b9 Mon Sep 17 00:00:00 2001 From: Craig Marshall Date: Sun, 26 Sep 2010 16:27:15 +0100 Subject: [PATCH] Guillotine Extension: Change output message to give full pathnames of output files Fix blank image name crash/bug Clean up whitespace Also - added my name to authors file --- AUTHORS | 1 + share/extensions/guillotine.py | 94 +++++++++++++++++++--------------- 2 files changed, 53 insertions(+), 42 deletions(-) diff --git a/AUTHORS b/AUTHORS index 736c46352..7c6ec9767 100644 --- a/AUTHORS +++ b/AUTHORS @@ -92,6 +92,7 @@ Ivan Louette Pierre-Antoine Marc Aurel-Aimé Marmion Colin Marquardt +Craig Marshall Dmitry G. Mastrukov Matiphas Michael Meeks diff --git a/share/extensions/guillotine.py b/share/extensions/guillotine.py index f0e3f4aaa..bda56b206 100644 --- a/share/extensions/guillotine.py +++ b/share/extensions/guillotine.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python ''' guillotine.py @@ -20,10 +20,10 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ----------------------- -This script slices an inkscape drawing along the guides, similarly to -the GIMP plugin called "guillotine". It can optionally export to the +This script slices an inkscape drawing along the guides, similarly to +the GIMP plugin called "guillotine". It can optionally export to the same directory as the SVG file with the same name, but with a number -suffix. e.g. +suffix. e.g. /home/foo/drawing.svg @@ -43,6 +43,8 @@ import sys import inkex import simplestyle import locale +import gettext +_ = gettext.gettext locale.setlocale(locale.LC_ALL, '') @@ -62,26 +64,26 @@ def float_sort(a, b): class Guillotine(inkex.Effect): """Exports slices made using guides""" def __init__(self): - inkex.Effect.__init__(self) - self.OptionParser.add_option("--directory", action="store", + inkex.Effect.__init__(self) + self.OptionParser.add_option("--directory", action="store", type="string", dest="directory", default=None, help="") - - self.OptionParser.add_option("--image", action="store", - type="string", dest="image", + + self.OptionParser.add_option("--image", action="store", + type="string", dest="image", default=None, help="") - - self.OptionParser.add_option("--ignore", action="store", - type="inkbool", dest="ignore", + + self.OptionParser.add_option("--ignore", action="store", + type="inkbool", dest="ignore", default=None, help="") - + def get_guides(self): ''' Returns all guide elements as an iterable collection ''' root = self.document.getroot() guides = [] - xpath = self.document.xpath("//sodipodi:guide", + xpath = self.document.xpath("//sodipodi:guide", namespaces=inkex.NSS) for g in xpath: guide = {} @@ -95,10 +97,10 @@ class Guillotine(inkex.Effect): guide['position'] = x guides.append(guide) return guides - + def get_all_horizontal_guides(self): ''' - Returns all horizontal guides as a list of floats stored as + Returns all horizontal guides as a list of floats stored as strings. Each value is the position from 0 in pixels. ''' guides = [] @@ -116,11 +118,11 @@ class Guillotine(inkex.Effect): for g in self.get_guides(): if g['orientation'] == 'vertical': guides.append(g['position']) - return guides - + return guides + def get_horizontal_slice_positions(self): ''' - Make a sorted list of all horizontal guide positions, + Make a sorted list of all horizontal guide positions, including 0 and the document height, but not including those outside of the canvas ''' @@ -131,14 +133,14 @@ class Guillotine(inkex.Effect): if h >= 0 and float(h) <= float(height): horizontals.append(h) horizontals.append(height) - horizontals.sort(cmp=float_sort) + horizontals.sort(cmp=float_sort) return horizontals - + def get_vertical_slice_positions(self): ''' Make a sorted list of all vertical guide positions, including 0 and the document width, but not including - those outside of the canvas. + those outside of the canvas. ''' root = self.document.getroot() verticals = ['0'] @@ -149,12 +151,12 @@ class Guillotine(inkex.Effect): verticals.append(width) verticals.sort(cmp=float_sort) return verticals - + def get_slices(self): ''' Returns a list of all "slices" as denoted by the guides - on the page. Each slice is really just a 4 element list of - floats (stored as strings), consisting of the X and Y start + on the page. Each slice is really just a 4 element list of + floats (stored as strings), consisting of the X and Y start position and the X and Y end position. ''' hs = self.get_horizontal_slice_positions() @@ -164,23 +166,26 @@ class Guillotine(inkex.Effect): for j in range(len(vs)-1): slices.append([vs[j], hs[i], vs[j+1], hs[i+1]]) return slices - + def get_filename_parts(self): ''' - Attempts to get directory and image as passed in by the inkscape + Attempts to get directory and image as passed in by the inkscape dialog. If the boolean ignore flag is set, then it will ignore these settings and try to use the settings from the export filename. ''' - + if self.options.ignore == False: + if self.options.image == "" or self.options.image is None: + inkex.errormsg("Please enter an image name") + sys.exit(0) return (self.options.directory, self.options.image) else: ''' - First get the export-filename from the document, if the + First get the export-filename from the document, if the document has been exported before (TODO: Will not work if it - hasn't been exported yet), then uses this to return a tuple - consisting of the directory to export to, and the filename + hasn't been exported yet), then uses this to return a tuple + consisting of the directory to export to, and the filename without extension. ''' svg = self.document.getroot() @@ -189,12 +194,12 @@ class Guillotine(inkex.Effect): export_file = svg.attrib[att] except KeyError: inkex.errormsg("To use the export hints option, you " + - "need to have previously exported the document. " + + "need to have previously exported the document. " + "Otherwise no export hints exist!") sys.exit(-1) dirname, filename = os.path.split(export_file) filename = filename.rsplit(".", 1)[0] # Without extension - return (dirname, filename) + return (dirname, filename) def check_dir_exists(self, dir): if not os.path.isdir(dir): @@ -205,8 +210,8 @@ class Guillotine(inkex.Effect): def export_slice(self, s, filename): ''' - Runs inkscape's command line interface and exports the image - slice from the 4 coordinates in s, and saves as the filename + Runs inkscape's command line interface and exports the image + slice from the 4 coordinates in s, and saves as the filename given. ''' svg_file = self.args[-1] @@ -219,30 +224,35 @@ class Guillotine(inkex.Effect): else: _, f, err = os.open3(command) f.close() - + def export_slices(self, slices): ''' - Takes the slices list and passes each one with a calculated + Takes the slices list and passes each one with a calculated filename/directory into export_slice. ''' dirname, filename = self.get_filename_parts() + output_files = list() if dirname == '' or dirname == None: dirname = './' - inkex.errormsg(dirname) + dirname = os.path.expanduser(dirname) dirname = os.path.expandvars(dirname) + dirname = os.path.abspath(dirname) + if dirname[-1] != os.path.sep: + dirname += os.path.sep self.check_dir_exists(dirname) i = 0 for s in slices: - f = dirname + os.path.sep + filename + str(i) + ".png" + f = dirname + filename + str(i) + ".png" + output_files.append(f) self.export_slice(s, f) i += 1 - + inkex.errormsg(_("The sliced bitmaps have been saved as:") + "\n\n" + "\n".join(output_files)) + def effect(self): slices = self.get_slices() self.export_slices(slices) - + if __name__ == "__main__": e = Guillotine() e.affect() - -- 2.30.2