def get_icon_html(icon: QIcon, size: QSize) -> str: """ Transform an icon to html <img> tag. """ if not size.isValid(): return "" if size.width() < 0 or size.height() < 0: size = QSize(16, 16) # just in case byte_array = QByteArray() buffer = QBuffer(byte_array) buffer.open(QIODevice.WriteOnly) pixmap = icon.pixmap(size) if pixmap.isNull(): return "" pixmap.save(buffer, "PNG") buffer.close() dpr = pixmap.devicePixelRatioF() if dpr != 1.0: size_ = pixmap.size() / dpr size_part = ' width="{}" height="{}"'.format( int(math.floor(size_.width())), int(math.floor(size_.height())) ) else: size_part = '' img_encoded = byte_array.toBase64().data().decode("utf-8") return '<img src="data:image/png;base64,{}"{}/>'.format(img_encoded, size_part)
def get_html_img(scene): """ Create HTML img element with base64-encoded image from the scene """ byte_array = QByteArray() filename = QBuffer(byte_array) filename.open(QIODevice.WriteOnly) PngFormat.write(filename, scene) img_encoded = byte_array.toBase64().data().decode("utf-8") return "<img src='data:image/png;base64,%s'/>" % img_encoded
def get_html_img(scene: QGraphicsScene, max_height: Optional[int] = None) -> str: """ Create HTML img element with base64-encoded image from the scene. If max_height is not none set the max height of the image in html. """ byte_array = QByteArray() filename = QBuffer(byte_array) filename.open(QIODevice.WriteOnly) PngFormat.write(filename, scene) img_encoded = byte_array.toBase64().data().decode("utf-8") return '<img {} src="data:image/png;base64,{}"/>'.format( ("" if max_height is None else 'style="max-height: {}px"'.format(max_height)), img_encoded)
def grab_svg(scene): # type: (QGraphicsScene) -> str """ Return a SVG rendering of the scene contents. Parameters ---------- scene : :class:`CanvasScene` """ svg_buffer = QBuffer() gen = _QSvgGenerator() gen.setOutputDevice(svg_buffer) items_rect = scene.itemsBoundingRect().adjusted(-10, -10, 10, 10) if items_rect.isNull(): items_rect = QRectF(0, 0, 10, 10) width, height = items_rect.width(), items_rect.height() rect_ratio = float(width) / height # Keep a fixed aspect ratio. aspect_ratio = 1.618 if rect_ratio > aspect_ratio: height = int(height * rect_ratio / aspect_ratio) else: width = int(width * aspect_ratio / rect_ratio) target_rect = QRectF(0, 0, width, height) source_rect = QRectF(0, 0, width, height) source_rect.moveCenter(items_rect.center()) gen.setSize(target_rect.size().toSize()) gen.setViewBox(target_rect) painter = QPainter(gen) # Draw background. painter.setPen(Qt.NoPen) painter.setBrush(scene.palette().base()) painter.drawRect(target_rect) # Render the scene scene.render(painter, target_rect, source_rect) painter.end() buffer_str = bytes(svg_buffer.buffer()) return buffer_str.decode("utf-8")
def image_data(pm): # type: (QPixmap) -> str """ Render the contents of the pixmap as a data URL (RFC-2397) Parameters ---------- pm : QPixmap Returns ------- datauri : str """ pm = QPixmap(pm) device = QBuffer() assert device.open(QBuffer.ReadWrite) pm.save(device, b"png") device.close() data = bytes(device.data()) payload = base64.b64encode(data).decode("ascii") return "data:image/png;base64," + payload
def test(self): b = QBuffer() w = QPdfWriter(b) size = QPageSize(QSizeF(10, 10), QPageSize.Millimeter) _ = w.setPageSize(size) self.assertTrue(w.setPageSize(size))