def test_mmPointConversion(self): """Test that conversions between pixel and page dimensions work.""" myDpi = 300 myPixels = 300 myMM = 25.4 # 1 inch myResult = points_to_mm(myPixels, myDpi) myMessage = "Expected: %s\nGot: %s" % (myMM, myResult) assert myResult == myMM, myMessage myResult = mm_to_points(myMM, myDpi) myMessage = "Expected: %s\nGot: %s" % (myPixels, myResult) assert myResult == myPixels, myMessage
def test_mm_to_points(self): """Test that conversions between pixel and page dimensions work.""" dpi = 300 myPixels = 300 mm = 25.4 # 1 inch result = points_to_mm(myPixels, dpi) message = "Expected: %s\nGot: %s" % (mm, result) assert result == mm, message result = mm_to_points(mm, dpi) message = "Expected: %s\nGot: %s" % (myPixels, result) assert result == myPixels, message
def html_to_image(self, html, width_mm): """Render some HTML to a pixmap. :param html: HTML to be rendered. It is assumed that the html is a snippet only, containing no body element - a standard header and footer will be appended. :type html: str :param width_mm: width of the table in mm - will be converted to points based on the resolution of our page. :type width_mm: int :returns: An image containing the rendered html. :rtype: QImage """ LOGGER.debug('InaSAFE Map renderHtmlToImage called') width_px = mm_to_points(width_mm, self.page_dpi) self.load_and_wait(html_snippet=html) frame = self.web_view.page().mainFrame() # Using 150dpi as the baseline, work out a standard text size # multiplier so that page renders equally well at different print # resolutions. #myBaselineDpi = 150 #myFactor = float(self.page_dpi) / myBaselineDpi #myFrame.setTextSizeMultiplier(myFactor) size = frame.contentsSize() size.setWidth(width_px) self.web_view.page().setViewportSize(size) image = QtGui.QImage(size, QtGui.QImage.Format_RGB32) image.setDotsPerMeterX(dpi_to_meters(self.page_dpi)) image.setDotsPerMeterY(dpi_to_meters(self.page_dpi)) # Only works in Qt4.8 #image.fill(QtGui.qRgb(255, 255, 255)) # Works in older Qt4 versions image.fill(255 + 255 * 256 + 255 * 256 * 256) painter = QtGui.QPainter(image) frame.render(painter) painter.end() return image
def html_to_image(self, html, width_mm): """Render some HTML to a pixmap. :param html: HTML to be rendered. It is assumed that the html is a snippet only, containing no body element - a standard header and footer will be appended. :type html: str :param width_mm: width of the table in mm - will be converted to points based on the resolution of our page. :type width_mm: int :returns: An image containing the rendered html. :rtype: QImage """ LOGGER.debug('InaSAFE Map renderHtmlToImage called') myWidthPx = mm_to_points(width_mm, self.pageDpi) self.load_and_wait(html_snippet=html) myFrame = self.webView.page().mainFrame() # Using 150dpi as the baseline, work out a standard text size # multiplier so that page renders equally well at different print # resolutions. #myBaselineDpi = 150 #myFactor = float(self.page_dpi) / myBaselineDpi #myFrame.setTextSizeMultiplier(myFactor) mySize = myFrame.contentsSize() mySize.setWidth(myWidthPx) self.webView.page().setViewportSize(mySize) myImage = QtGui.QImage(mySize, QtGui.QImage.Format_RGB32) myImage.setDotsPerMeterX(dpi_to_meters(self.pageDpi)) myImage.setDotsPerMeterY(dpi_to_meters(self.pageDpi)) # Only works in Qt4.8 #myImage.fill(QtGui.qRgb(255, 255, 255)) # Works in older Qt4 versions myImage.fill(255 + 255 * 256 + 255 * 256 * 256) myPainter = QtGui.QPainter(myImage) myFrame.render(myPainter) myPainter.end() myImage.save('/tmp/test.png') return myImage
def renderHtmlToImage(self, theHtml, theWidthMM): """Render some HTML to a pixmap. Args: * theHtml - HTML to be rendered. It is assumed that the html is a snippet only, containing no body element - a standard header and footer will be appended. * theWidthMM- width of the table in mm - will be converted to points based on the resolution of our page. Returns: QImage Raises: Any exceptions raised by the InaSAFE library will be propagated. """ LOGGER.debug('InaSAFE Map renderHtmlToImage called') myWidthPx = mm_to_points(theWidthMM, self.pageDpi) self.loadAndWait(theHtmlSnippet=theHtml) myFrame = self.webView.page().mainFrame() # Using 150dpi as the baseline, work out a standard text size # multiplier so that page renders equally well at different print # resolutions. #myBaselineDpi = 150 #myFactor = float(self.pageDpi) / myBaselineDpi #myFrame.setTextSizeMultiplier(myFactor) mySize = myFrame.contentsSize() mySize.setWidth(myWidthPx) self.webView.page().setViewportSize(mySize) myImage = QtGui.QImage(mySize, QtGui.QImage.Format_RGB32) myImage.setDotsPerMeterX(dpi_to_meters(self.pageDpi)) myImage.setDotsPerMeterY(dpi_to_meters(self.pageDpi)) # Only works in Qt4.8 #myImage.fill(QtGui.qRgb(255, 255, 255)) # Works in older Qt4 versions myImage.fill(255 + 255 * 256 + 255 * 256 * 256) myPainter = QtGui.QPainter(myImage) myFrame.render(myPainter) myPainter.end() myImage.save('/tmp/test.png') return myImage
def draw_image(self, theImage, theWidthMM, theLeftOffset, theTopOffset): """Helper to draw an image directly onto the QGraphicsScene. This is an alternative to using QgsComposerPicture which in some cases leaves artifacts under windows. The Pixmap will have a transform applied to it so that it is rendered with the same resolution as the composition. :param theImage: Image that will be rendered to the layout. :type theImage: QImage :param theWidthMM: Desired width in mm of output on page. :type theWidthMM: int :param theLeftOffset: Offset from left of page. :type theLeftOffset: int :param theTopOffset: Offset from top of page. :type theTopOffset: int :returns: Graphics scene item. :rtype: QGraphicsSceneItem """ LOGGER.debug('InaSAFE Map drawImage called') myDesiredWidthMM = theWidthMM # mm myDesiredWidthPX = mm_to_points(myDesiredWidthMM, self.pageDpi) myActualWidthPX = theImage.width() myScaleFactor = myDesiredWidthPX / myActualWidthPX LOGGER.debug('%s %s %s' % ( myScaleFactor, myActualWidthPX, myDesiredWidthPX)) myTransform = QtGui.QTransform() myTransform.scale(myScaleFactor, myScaleFactor) myTransform.rotate(0.5) # noinspection PyArgumentList myItem = self.composition.addPixmap(QtGui.QPixmap.fromImage(theImage)) myItem.setTransform(myTransform) myItem.setOffset(theLeftOffset / myScaleFactor, theTopOffset / myScaleFactor) return myItem
def draw_image(self, image, width_mm, left_offset, top_offset): """Helper to draw an image directly onto the QGraphicsScene. This is an alternative to using QgsComposerPicture which in some cases leaves artifacts under windows. The Pixmap will have a transform applied to it so that it is rendered with the same resolution as the composition. :param image: Image that will be rendered to the layout. :type image: QImage :param width_mm: Desired width in mm of output on page. :type width_mm: int :param left_offset: Offset from left of page. :type left_offset: int :param top_offset: Offset from top of page. :type top_offset: int :returns: Graphics scene item. :rtype: QGraphicsSceneItem """ LOGGER.debug('InaSAFE Map drawImage called') desired_width_mm = width_mm # mm desired_width_px = mm_to_points(desired_width_mm, self.page_dpi) actual_width_px = image.width() scale_factor = desired_width_px / actual_width_px LOGGER.debug('%s %s %s' % ( scale_factor, actual_width_px, desired_width_px)) transform = QtGui.QTransform() transform.scale(scale_factor, scale_factor) transform.rotate(0.5) # noinspection PyArgumentList item = self.composition.addPixmap(QtGui.QPixmap.fromImage(image)) item.setTransform(transform) item.setOffset( left_offset / scale_factor, top_offset / scale_factor) return item
def draw_image(self, theImage, theWidthMM, theLeftOffset, theTopOffset): """Helper to draw an image directly onto the QGraphicsScene. This is an alternative to using QgsComposerPicture which in some cases leaves artifacts under windows. The Pixmap will have a transform applied to it so that it is rendered with the same resolution as the composition. :param theImage: Image that will be rendered to the layout. :type theImage: QImage :param theWidthMM: Desired width in mm of output on page. :type theWidthMM: int :param theLeftOffset: Offset from left of page. :type theLeftOffset: int :param theTopOffset: Offset from top of page. :type theTopOffset: int :returns: Graphics scene item. :rtype: QGraphicsSceneItem """ LOGGER.debug('InaSAFE Map drawImage called') myDesiredWidthMM = theWidthMM # mm myDesiredWidthPX = mm_to_points(myDesiredWidthMM, self.pageDpi) myActualWidthPX = theImage.width() myScaleFactor = myDesiredWidthPX / myActualWidthPX LOGGER.debug('%s %s %s' % (myScaleFactor, myActualWidthPX, myDesiredWidthPX)) myTransform = QtGui.QTransform() myTransform.scale(myScaleFactor, myScaleFactor) myTransform.rotate(0.5) # noinspection PyArgumentList myItem = self.composition.addPixmap(QtGui.QPixmap.fromImage(theImage)) myItem.setTransform(myTransform) myItem.setOffset(theLeftOffset / myScaleFactor, theTopOffset / myScaleFactor) return myItem
def draw_image(self, image, width_mm, left_offset, top_offset): """Helper to draw an image directly onto the QGraphicsScene. This is an alternative to using QgsComposerPicture which in some cases leaves artifacts under windows. The Pixmap will have a transform applied to it so that it is rendered with the same resolution as the composition. :param image: Image that will be rendered to the layout. :type image: QImage :param width_mm: Desired width in mm of output on page. :type width_mm: int :param left_offset: Offset from left of page. :type left_offset: int :param top_offset: Offset from top of page. :type top_offset: int :returns: Graphics scene item. :rtype: QGraphicsSceneItem """ LOGGER.debug('InaSAFE Map drawImage called') desired_width_mm = width_mm # mm desired_width_px = mm_to_points(desired_width_mm, self.page_dpi) actual_width_px = image.width() scale_factor = desired_width_px / actual_width_px LOGGER.debug('%s %s %s' % (scale_factor, actual_width_px, desired_width_px)) transform = QtGui.QTransform() transform.scale(scale_factor, scale_factor) transform.rotate(0.5) # noinspection PyArgumentList item = self.composition.addPixmap(QtGui.QPixmap.fromImage(image)) item.setTransform(transform) item.setOffset(left_offset / scale_factor, top_offset / scale_factor) return item