def load_image(self, data): self.is_valid = False try: fmt = identify_data(data)[-1].encode('ascii') except Exception: fmt = b'' self.original_image_format = fmt.decode('ascii').lower() self.selection_state.reset() self.original_image_data = data self.current_image = i = self.original_image = ( QImage.fromData(data, format=fmt) if fmt else QImage.fromData(data)) self.is_valid = not i.isNull() self.update() self.image_changed.emit(self.current_image)
def magick_to_qimage(img): fmt = get_pixel_map() # ImageMagick can only output raw data in some formats that can be # read into QImage directly, if the QImage format is not one of those, use # PNG if fmt in {'RGBA', 'BGRA'}: w, h = img.size img.depth = 8 # QImage expects 8bpp raw = img.export(fmt) i = QImage(raw, w, h, QImage.Format_ARGB32) del raw # According to the documentation, raw is supposed to not be deleted, but it works, so make it explicit return i else: raw = img.export('PNG') return QImage.fromData(QByteArray(raw), 'PNG')
def copyToClipboard(self): with BytesIO() as buffer: self._figure.savefig(buffer, facecolor=self._figure.get_facecolor()) QApplication.clipboard().setImage( QImage.fromData(buffer.getvalue()))