def svg(self, filename, rect=None, resolution=72.0, paperColor=None): """Create a SVG file for the selected rect or the whole page. The filename may be a string or a QIODevice object. The rectangle is relative to our top-left position. Normally vector graphics are rendered, but in cases where that is not possible, the resolution will be used to determine the DPI for the generated rendering. """ # map to the original page source = self.pageRect() if rect is None else self.mapFromPage().rect( rect) # scale to target size w = source.width() * self.scaleX h = source.height() * self.scaleY if self.computedRotation & 1: w, h = h, w targetSize = QSizeF(w, h) * resolution / self.dpi svg = QSvgGenerator() if isinstance(filename, str): svg.setFileName(filename) else: svg.setOutputDevice(filename) svg.setResolution(int(resolution)) svg.setSize(targetSize.toSize()) svg.setViewBox(QRectF(0, 0, targetSize.width(), targetSize.height())) return self.output(svg, source, paperColor)
def save(self, path, filetype=None, title=None, description=None, svg_dpi=SVG_INKSCAPE): if path is None or not isinstance(path, str): logger.error('please provide a valide path to save the image "' + str(path) + '"') return if filetype is None: if path.lower().endswith('.svg'): filetype = 'svg' else: filetype = 'raster' dpi = 72 # 300 # inkscape 96 ? check for illustrator --> check if filetype == 'svg': generator = QSvgGenerator() generator.setFileName(path) if svg_dpi == self.SVG_ILLUSTRATOR: generator.setSize(QSize(595, 842)) generator.setViewBox(QRect(0, 0, 595, 842)) else: generator.setSize(QSize(794, 1123)) generator.setViewBox(QRect(0, 0, 794, 1123)) if title is not None and isinstance(title, str): generator.setTitle(title) if description is not None and isinstance(description, str): generator.setDescription(description) generator.setResolution( svg_dpi ) # fixes issues in inkscape of pt size --> 72 pr illustrator and 96 pr inkscape but need change size painter = QPainter(generator) print(generator.title(), generator.heightMM(), generator.height(), generator.widthMM(), generator.resolution(), generator.description(), generator.logicalDpiX()) else: scaling_factor_dpi = 1 scaling_factor_dpi = self.scaling_factor_to_achieve_DPI(300) image = QtGui.QImage( QSize( self.cm_to_inch(21) * dpi * scaling_factor_dpi, self.cm_to_inch(29.7) * dpi * scaling_factor_dpi), QtGui.QImage.Format_RGB32) painter = QPainter( image) # see what happens in case of rounding of pixels painter.scale(scaling_factor_dpi, scaling_factor_dpi) painter.setRenderHint( QPainter.HighQualityAntialiasing ) # to improve rendering #Antialiasing otherwise or nothing self.paint(painter) painter.end() if filetype != 'svg': image.save(path)
def exportSvg(self): """ For exporting a svg file (text is still too large) Returns ------- """ generator = QSvgGenerator() generator.setResolution(300) generator.setSize( QSize(self.diagramScene.width(), self.diagramScene.height())) # generator.setViewBox(QRect(0, 0, 800, 800)) generator.setViewBox(self.diagramScene.sceneRect()) generator.setFileName("VectorGraphicsExport.svg") painter = QPainter() painter.begin(generator) painter.setRenderHint(QPainter.Antialiasing) self.diagramScene.render(painter) painter.end()
def save(self, path, filetype=None, title=None, description=None, svg_dpi=SVG_INKSCAPE): if path is None or not isinstance(path, str): logger.error('please provide a valide path to save the image "' + str(path) + '"') return if filetype is None: if path.lower().endswith('.svg'): filetype = 'svg' else: filetype = os.path.splitext(path)[1] dpi = 72 # 300 # inkscape 96 ? check for illustrator --> check if filetype == 'svg': generator = QSvgGenerator() generator.setFileName(path) if svg_dpi == self.SVG_ILLUSTRATOR: generator.setSize(QSize(595, 842)) generator.setViewBox(QRect(0, 0, 595, 842)) else: generator.setSize(QSize(794, 1123)) generator.setViewBox(QRect(0, 0, 794, 1123)) if title is not None and isinstance(title, str): generator.setTitle(title) if description is not None and isinstance(description, str): generator.setDescription(description) generator.setResolution( svg_dpi) # fixes issues in inkscape of pt size --> 72 pr illustrator and 96 pr inkscape but need change size painter = QPainter(generator) # print(generator.title(), generator.heightMM(), generator.height(), generator.widthMM(), # generator.resolution(), generator.description(), generator.logicalDpiX()) else: scaling_factor_dpi = 1 scaling_factor_dpi = self.scaling_factor_to_achieve_DPI(300) # in fact take actual page size ??? multiplied by factor # just take real image size instead # image = QtGui.QImage(QSize(self.cm_to_inch(21) * dpi * scaling_factor_dpi, self.cm_to_inch(29.7) * dpi * scaling_factor_dpi), QtGui.QImage.Format_RGBA8888) # minor change to support alpha # QtGui.QImage.Format_RGB32) # NB THE FOLLOWING LINES CREATE A WEIRD ERROR WITH WEIRD PIXELS DRAWN some sort of lines NO CLUE WHY img_bounds = self.getBounds() image = QtGui.QImage(QSize(img_bounds.width() * scaling_factor_dpi, img_bounds.height()* scaling_factor_dpi), QtGui.QImage.Format_RGBA8888) # minor change to support alpha # QtGui.QImage.Format_RGB32) # print('size at dpi',QSize(img_bounds.width() * scaling_factor_dpi, img_bounds.height()* scaling_factor_dpi)) # QSize(self.cm_to_inch(0.02646 * img_bounds.width()) # self.cm_to_inch(0.02646 * img_bounds.height()) # need convert pixels to inches # is there a rounding error # force white bg for non jpg try: # print(filetype.lower()) # the tif and png file formats support alpha if not filetype.lower() == '.png' and not filetype.lower() == '.tif' and not filetype.lower() == '.tiff': image.fill(QColor.fromRgbF(1,1,1)) else: # image.fill(QColor.fromRgbF(1, 1, 1, alpha=1)) # image.fill(QColor.fromRgbF(1, 1, 1, alpha=1)) # TODO KEEP in fact image need BE FILLED WITH TRANSPARENT OTHERWISE GETS WEIRD DRAWING ERRORS # TODO KEEP SEE https://stackoverflow.com/questions/13464627/qt-empty-transparent-qimage-has-noise # image.fill(qRgba(0, 0, 0, 0)) image.fill(QColor.fromRgbF(0,0,0,0)) except: pass painter = QPainter(image) # see what happens in case of rounding of pixels # painter.begin() painter.scale(scaling_factor_dpi, scaling_factor_dpi) painter.setRenderHint(QPainter.HighQualityAntialiasing) # to improve rendering quality self.paint(painter) painter.end() if filetype != 'svg': image.save(path)