def __init__(self, filename, width=None, height=None, kind='direct', mask=None, lazy=True, srcinfo=None): Flowable.__init__(self) self._kind = kind self._mode = 'svg2rlg' self.doc = svg2rlg(filename) self.imageWidth = width self.imageHeight = height x1, y1, x2, y2 = self.doc.getBounds() # Actually, svg2rlg's getBounds seems broken. self._w, self._h = x2, y2 if not self.imageWidth: self.imageWidth = self._w if not self.imageHeight: self.imageHeight = self._h self.__ratio = float(self.imageWidth) / self.imageHeight if kind in ['direct', 'absolute']: self.drawWidth = width or self.imageWidth self.drawHeight = height or self.imageHeight elif kind in ['bound', 'proportional']: factor = min( float(width) / self.imageWidth, float(height) / self.imageHeight) self.drawWidth = self.imageWidth * factor self.drawHeight = self.imageHeight * factor
def svgConvert(inPathFileName, outPathFileName, JPG=True, PNG=None): drawing = svg2rlg(inPathFileName + '.svg') renderPDF.drawToFile(drawing, outPathFileName + ".pdf") if JPG: renderPM.drawToFile(drawing, outPathFileName + ".jpg", 'JPG') if PNG: renderPM.drawToFile(drawing, outPathFileName + ".png", 'PNG')
def __init__(self, filename, width=None, height=None, kind='direct', mask=None, lazy=True, srcinfo=None): Flowable.__init__(self) self._kind = kind self._mode = 'svg2rlg' self.doc = svg2rlg(filename) self.imageWidth = width self.imageHeight = height x1, y1, x2, y2 = self.doc.getBounds() # Actually, svg2rlg's getBounds seems broken. self._w, self._h = x2, y2 if not self.imageWidth: self.imageWidth = self._w if not self.imageHeight: self.imageHeight = self._h self.__ratio = float(self.imageWidth) / self.imageHeight if kind in ['direct', 'absolute']: self.drawWidth = width or self.imageWidth self.drawHeight = height or self.imageHeight elif kind in ['bound', 'proportional']: factor = min( float(width) / self.imageWidth, float(height) / self.imageHeight ) self.drawWidth = self.imageWidth * factor self.drawHeight = self.imageHeight * factor
def mcviz(inputhepmc,outputsvg,outputpdf,outputpng): try: subprocess.check_call(['mcviz','--demo',inputhepmc,'--output_file',outputsvg]) import svg2rlg import reportlab.graphics d = svg2rlg.svg2rlg(outputsvg);reportlab.graphics.renderPDF.drawToFile(d,outputpdf) subprocess.check_call(['convert','-trim','-density','3000',outputpdf,outputpng]) except subprocess.CalledProcessError: log.error('mcviz failed!') raise RuntimeError
def OnRender(self, event): # show expected png image path, filename = os.path.split(self.test_name) name, ext = os.path.splitext(filename) imgpath = None for prefix in ('', 'basic-', 'full-', 'tiny-'): imgpath = os.path.join(os.getcwd(), 'test-suite', 'png', prefix + name + '.png') if os.path.exists(imgpath): break if not imgpath is None: img = wx.Image(imgpath) self.rightimage.SetImage(img) else: self.log.Write('Could not load image: "%s"' % name) # render svg file start = time.clock() try: if USESVGLIB: drawing = svglib.svg2rlg(self.test_name) else: drawing = svg2rlg(self.test_name) except: type, value, traceback = sys.exc_info() msg = str(value) dlg = wx.MessageDialog( self, msg, "svg2rlg", wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP) dlg.Raise() dlg.ShowModal() dlg.Destroy() return delta = time.clock() - start self.log.Write('Rendered in %.2f seconds' % delta) fh, tmp = tempfile.mkstemp(suffix='.png', prefix="svg2rlg_") os.close(fh) path, filename = os.path.split(tmp) name, ext = os.path.splitext(filename) drawing.save(formats=['png'], outDir=path, fnRoot=name) img = wx.Image(os.path.join(path, name + '.png')) self.leftimage.SetImage(img)
def OnRender(self, event): # show expected png image path, filename = os.path.split(self.test_name) name, ext = os.path.splitext(filename) imgpath = None for prefix in ('', 'basic-', 'full-', 'tiny-'): imgpath = os.path.join(os.getcwd(), 'test-suite', 'png', prefix + name + '.png') if os.path.exists(imgpath): break if not imgpath is None: img = wx.Image(imgpath) self.rightimage.SetImage(img) else: self.log.Write('Could not load image: "%s"' % name) # render svg file start = time.clock() try: if USESVGLIB: drawing = svglib.svg2rlg(self.test_name) else: drawing = svg2rlg(self.test_name) except: type, value, traceback = sys.exc_info() msg = str(value) dlg = wx.MessageDialog(self, msg, "svg2rlg", wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP) dlg.Raise() dlg.ShowModal() dlg.Destroy() return delta = time.clock() - start self.log.Write('Rendered in %.2f seconds' % delta) fh, tmp = tempfile.mkstemp(suffix = '.png', prefix = "svg2rlg_") os.close(fh) path, filename = os.path.split(tmp) name, ext = os.path.splitext(filename) drawing.save(formats=['png'],outDir=path,fnRoot=name) img = wx.Image(os.path.join(path, name + '.png')) self.leftimage.SetImage(img)