def _resizeImage(self, virtual_path: str, width: int, height: int) -> IO[bytes]: input = self.getStream(virtual_path) try: from PyQt5.QtGui import QImage from PyQt5.QtCore import Qt, QBuffer image = QImage() image.loadFromData(input.read()) image = image.scaled(width, height, Qt.IgnoreAspectRatio, Qt.SmoothTransformation) output_buffer = QBuffer() output_buffer.open(QBuffer.ReadWrite) image.save(output_buffer, "PNG") output_buffer.seek(0) # Reset that buffer so that the next guy can request it. return BytesIO(output_buffer.readAll()) except ImportError: # TODO: Try other image loaders. raise # Raise import error again if we find no other image loaders.
def image_section(section_e: "ImageSection") -> Tuple[str, str]: section = section_e.to_dict() image = create_images_with_annotation(section) width, height = get_image_size(image) buf = QBuffer() buf.open(QBuffer.ReadWrite) image.save(buf, "PNG") buf.seek(0) content = buf.readAll().toBase64() buf.close() res = f"""<text:p text:style-name="Standard"> <draw:frame draw:style-name="fr1" draw:name="{uuid.uuid4().hex}" text:anchor-type="paragraph" svg:width="{int(width)}mm" svg:height="{int(height)}mm" draw:z-index="0"> <draw:image loext:mime-type="image/png"> <office:binary-data>{content.data().decode()}</office:binary-data> </draw:image> </draw:frame> </text:p>""" return res, ""
def edit_paste_as(self): """Pastes clipboard into one cell using a user specified mime type""" grid = self.main_window.grid model = grid.model # The mimetypes that are supported by pyspread mimetypes = ("image", "text/html", "text/plain") clipboard = QApplication.clipboard() formats = clipboard.mimeData().formats() items = [fmt for fmt in formats if any(m in fmt for m in mimetypes)] if not items: return elif len(items) == 1: item = items[0] else: item, ok = QInputDialog.getItem(self.main_window, "Paste as", "Choose mime type", items, current=0, editable=False) if not ok: return row, column, table = current = grid.current # Target cell key description_tpl = "Paste {} from clipboard into cell {}" description = description_tpl.format(item, current) index = model.index(row, column, QModelIndex()) mime_data = clipboard.mimeData() if item == "image/svg+xml": # SVG Image if mime_data: svg = mime_data.data("image/svg+xml") self._paste_svg(str(svg, encoding='utf-8'), index) elif "image" in item and mime_data.hasImage(): # Bitmap Image image = clipboard.image() buffer = QBuffer() buffer.open(QBuffer.ReadWrite) image.save(buffer, "PNG") buffer.seek(0) image_data = buffer.readAll() buffer.close() self._paste_image(image_data, index) elif item == "text/html" and mime_data.hasHtml(): # HTML content html = mime_data.html() command = CommandSetCellCode(html, model, index, description) self.main_window.undo_stack.push(command) grid.on_markup_renderer_pressed(True) elif item == "text/plain": # Normal code code = clipboard.text() if code: command = CommandSetCellCode(code, model, index, description) self.main_window.undo_stack.push(command) else: # Unknown mime type return NotImplemented