summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 8d4a9c9)
raw | patch | inline | side by side (parent: 8d4a9c9)
author | Craig Marshall <craig9@gmail.com> | |
Sun, 26 Sep 2010 15:27:15 +0000 (16:27 +0100) | ||
committer | Craig Marshall <craig9@gmail.com> | |
Sun, 26 Sep 2010 15:27:15 +0000 (16:27 +0100) |
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
Fix blank image name crash/bug
Clean up whitespace
Also - added my name to authors file
AUTHORS | patch | blob | history | |
share/extensions/guillotine.py | patch | blob | history |
index 736c46352d6cbbe9c829f4ca2ac57493b26c1f52..7c6ec9767f89560978b0905da5d12bc47fed2327 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
Pierre-Antoine Marc
Aurel-Aimé Marmion
Colin Marquardt
+Craig Marshall
Dmitry G. Mastrukov
Matiphas
Michael Meeks
index f0e3f4aaae0944a59cb99697c8a2d3b688221139..bda56b206114c56238f826d367c059d8f2ccc0ea 100644 (file)
-#!/usr/bin/env python
+#!/usr/bin/env python
'''
guillotine.py
-----------------------
-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
import inkex
import simplestyle
import locale
+import gettext
+_ = gettext.gettext
locale.setlocale(locale.LC_ALL, '')
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 = {}
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 = []
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
'''
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']
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()
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()
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):
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]
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()
-