def export_node(self, node, file_name, width, height,area): """ Eating the stderr, so it doesn't show up in a error box after running the script. """ svg_file = self.temp_svg_file.name #self.args[-1] node_id = node.attrib['id'] directory = self.options.directory filename = os.path.join(directory, file_name) #area_string = '%f:%f:%f:%f' % ( float(area[0]), float(area[1]), float(area[0]) + float(area[2]), float(area[1]) + float(area[3])) if self.options.overwrite or not os.path.exists(filename): command = "%s -z -i %s -j -D -e %s %s -h %s -w %s" % (INKSCAPE_PATH, node_id, filename, svg_file, height, width) # print command if bsubprocess: p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) return_code = p.wait() f = p.stdout err = p.stderr logging.log(logging.DEBUG, "COMMAND %s returned %d %s %s" % (command,return_code,f.read(), err.read())) else: _, f, err = os.open3(command) logging.log(logging.DEBUG, " Error COMMAND %s" % (command,err)) f.close() else: logging.log(logging.DEBUG, "Export exists (%s) not overwriting" % filename)
def export_slice(self, s, filename, ratio): ''' Runs inkscape's command line interface and exports the image slice from the 4 coordinates in s, and saves as the filename given. ''' self.check_dir_exists(os.path.dirname(filename)) keepAspect = self.options.keepAspect; svg_file = self.args[-1] width = self.options.size * ratio height = width; if keepAspect: imgW = float(s[2])-float(s[0]) imgH = float(s[3])-float(s[1]) if imgH > imgW: width = (height/imgH) * imgW else: height = (width/imgW) * imgH command = "inkscape -a %s:%s:%s:%s -w %s -h %s -e \"%s\" \"%s\" " % (self.get_localised_string(s[0]), self.get_localised_string(s[1]), self.get_localised_string(s[2]), self.get_localised_string(s[3]), width, height, filename, svg_file) if bsubprocess: p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) return_code = p.wait() f = p.stdout err = p.stderr else: _, f, err = os.open3(command) f.close()
def convert_xbm(self, in_file_name, out_file_name): directory = self.options.directory in_filename = os.path.join(directory, in_file_name) out_filename = os.path.join(directory, out_file_name) if self.options.overwrite or not os.path.exists(in_filename): if USE_WAND: from wand.image import Image with Image(filename=in_filename) as img: img.format = 'xbm' img.save(filename=out_filename) else: command = "%s %s %s" % (CONVERT_PATH, in_filename, out_filename) # print command if bsubprocess: p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE,env=env) return_code = p.wait() f = p.stdout err = p.stderr logging.log(logging.DEBUG, "COMMAND %s returned %d %s %s" % (command,return_code,f.read(), err.read())) else: _, f, err = os.open3(command) logging.log(logging.DEBUG, "COMMAND %s" % command) f.close() else: logging.log(logging.DEBUG, "Export exists (%s) not overwriting" % out_filename)
def export_node(self, node, file_name, height, width): """ Eating the stderr, so it doesn't show up in a error box after running the script. """ svg_file = self.args[-1] node_id = node.attrib['id'] directory = self.options.directory filename = os.path.join(directory, file_name) color = '#555555' # grey - skipping if self.options.overwrite or not os.path.exists(filename): color = '#ff0000' # red - overwritten if not os.path.exists(filename): color = '#00ff00' # green - new export command = "inkscape -i %s -e %s %s -h %s -w %s" % (node_id, filename, svg_file, height, width) if bsubprocess: p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) return_code = p.wait() f = p.stdout err = p.stderr else: _, f, err = os.open3(command) logging.log(logging.DEBUG, "COMMAND %s" % command) f.close() else: logging.log(logging.DEBUG, "Export exists (%s) not overwriting" % filename) self.color_map[node_id] = color
def effect(self): self.svg = self.document.getroot() fromframe = self.options.fromframe toframe = self.options.toframe svg_file = self.args[-1] docname = self.xpathSingle('/svg:svg/@sodipodi:docname')[:-4] dirname = os.path.dirname(self.options.directory) image = self.options.image hpencil = self.options.hpencil if dirname == '' or dirname == None: dirname = './' dirname = os.path.expanduser(dirname) dirname = os.path.expandvars(dirname) #self.check_dir_exists(dirname) commands = StringIO() log = '' # iterate through the xml whenever the layer name is between # fromframe and toframe then edit the xml for node in self.svg.iter(): tag = node.tag.split("}")[1] if node.tag == inkex.addNS('g','svg'): idattr = node.attrib['id'] frametype = idattr[:-3] frame = idattr[-3:] try: framenum = int(frame) except: continue log += 'idattr:%s type:%s frame:%s\n' % (idattr, frametype, frame) if fromframe <= framenum <= toframe: if frametype == 'f': self.sethide(node, False) filename = dirname + os.path.sep + image + frame + ".png" commands.write("%s -i %s -j -C -e %s\n" % (svg_file, idattr, filename)) if frametype == 'pencil': self.sethide(node, hpencil) #TODO frames not being set to show commands.write("quit\n") if bsubprocess: echo = Popen(['echo', commands.getvalue()], shell=False, stdout=PIPE) ink = Popen(['inkscape', '--shell'], shell=False, stdin=echo.stdout, stdout=PIPE, stderr=PIPE) else: #reference: http://stackoverflow.com/questions/7442665/convert-svg-file-to-multiple-different-size-png-files _, ink, err = os.open3('inkscape --shell <<COMMANDS\n%sCOMMANDS' % (commands)) ink.close() stdoutdata, stderrdata = ink.communicate() log += stdoutdata
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 given. ''' svg_file = self.args[-1] command = "inkscape -a %s:%s:%s:%s -e \"%s\" \"%s\" " % (self.get_localised_string(s[0]), self.get_localised_string(s[1]), self.get_localised_string(s[2]), self.get_localised_string(s[3]), filename, svg_file) if bsubprocess: p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) return_code = p.wait() f = p.stdout err = p.stderr else: _, f, err = os.open3(command) f.close()
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 given. ''' svg_file = self.args[-1] command = "inkscape -d " + self.options.dpi + " -a %s:%s:%s:%s -e \"%s\" \"%s\" " % ( self.get_localised_string(s[0]), self.get_localised_string( s[1]), self.get_localised_string( s[2]), self.get_localised_string(s[3]), filename, svg_file) if bsubprocess: p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) return_code = p.wait() f = p.stdout err = p.stderr else: _, f, err = os.open3(command) f.close()
def effect(self): foreground = self.options.foreground background = self.options.background dirname = self.parse_dir(self.options.directory) output_name = os.path.join(dirname, self.parse_name(self.options.output_name)) tempdir = tempfile.mkdtemp() # to store intermediate files pdfmarks_file = os.path.join(tempdir,'pdfmarks') pdfmarks = open(pdfmarks_file,'w') # Create pdfmarks information pdfmarks.write("[ /Title ({0})\n".format(self.options.title)) if self.options.author != '': pdfmarks.write(" /Author ({0})\n".format(self.options.author)) if self.options.subject != '': pdfmarks.write(" /Subject ({0})\n".format(self.options.subject)) if self.options.keywords != '': pdfmarks.write(" /Keywords ({0})\n".format(self.options.keywords)) pdfmarks.write(" /DOCINFO pdfmark\n") pdfmarks.write("[/PageMode /UseOutlines /View [/Fit ] /Page 1 /DOCVIEW pdfmark\n") if DEBUG: inkex.errormsg(""" Directory {0} Output name {1} Temporary directory {2} Pdfmarks {3} """.format(dirname, output_name, tempdir, pdfmarks_file)) # Show always visible layer, and parse all the layer always_visible = (background, foreground) # list layers always visible layer_list = [] for layer in self.document.xpath('//svg:g[@inkscape:groupmode="layer"]', namespaces=NSS) : label = layer.xpath('@inkscape:label', namespaces=NSS)[0] if label in always_visible: layer.set("style", "display:inline") else: layer_list.append((label,layer)) layer.set("style", "display:none") i = 0 anime_pattern_search = re.compile('.*-[0-9]+$') anime_pattern_split = re.compile('-[0-9]+$') for label, layer in layer_list: if DEBUG: inkex.errormsg("Parsing layer " + label) # Ignore hidden layer, with name starting with '#' if '#' == label[0] : continue # Write bookmarks pdfmarks.write("[/Title ({0}) /Page {1} /OUT pdfmark\n".format(label, i+1)) layer.set("style", "display:inline") # Deal with animation, search for layers name .*-[0-9]+$ # Show all the step smaller than current animation number # and store animated (shown) layer in a list animated = [] if re.match(anime_pattern_search, label): current_step = int(label.split('-')[-1]) # label without numbers anime = re.split(anime_pattern_split, label)[0] current_anime_pattern = re.compile(anime + '\-[0-9]+$') for label2, layer2 in layer_list: if re.match(current_anime_pattern, label2): step = int(label2.split('-')[-1]) if step < current_step: layer2.set("style", "display:inline") animated.append(layer2) # TODO / FIXME # Clearing layer would save time, but would need reparsing all # the svg element... # Not working: #for other_layer in layer_dic.itervalues(): #if not other_layer is layer: #other_layer.clear() # FIXME # Following is a ugly and very slow hack: # Since we cannot call inkscape verbs within extension we have to # write, open and export each layer calling (and loading) # each time inkscape with the resulting file filename = os.path.join(tempdir, label + ".svg") self.document.write(filename) filename_pdf = os.path.join(tempdir, "export_%03i_" % i + label.replace(' ','_') + ".pdf") command = 'inkscape "{0}" --without-gui --export-area-page --export-dpi=300 --export-pdf="{1}"'.format(filename, filename_pdf) if bsubprocess: p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) return_code = p.wait() f = p.stdout err = p.stderr else: _, f, err = os.open3(command) f.close() if not KEEPTEMP: os.remove(filename) layer.set("style", "display:none") # Hide animated layers for layer2 in animated: layer2.set("style", "display:none") i+=1 pdfmarks.close() # Let's now join the pdf command = 'gs -r120 -dBATCH -dNOPAUSE -dPDFSETTINGS=/prepress -dAutoRotatePages=/None -sPAPERSIZE=A4 -sDEVICE=pdfwrite -sOutputFile="{0}" {1}/export_[0-9][0-9][0-9]_*.pdf "{2}"'.format( output_name, tempdir, pdfmarks_file) if bsubprocess: p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) return_code = p.wait() f = p.stdout err = p.stderr else: _, f, err = os.open3(command) f.close() # Store pdfmarks if OUTPUTPDFMARKS: import shutil shutil.move(pdfmarks_file, output_name+'.marks') # Clean temps if not KEEPTEMP: for f in os.listdir(tempdir): os.remove(os.path.join(tempdir,f)) os.removedirs(tempdir) else: inkex.errormsg('Temporary files stored in %s' % tempdir)