def from_qimage(self, img): from PyQt5.Qt import QImage fmt = get_pixel_map() if not img.hasAlphaChannel(): if img.format() != img.Format_RGB32: img = img.convertToFormat(QImage.Format_RGB32) fmt = fmt.replace('A', 'P') else: if img.format() != img.Format_ARGB32: img = img.convertToFormat(QImage.Format_ARGB32) raw = img.constBits().ascapsule() self.constitute(img.width(), img.height(), fmt, raw)
def to_qimage(self): from PyQt5.Qt import QImage, QByteArray 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 = self.size self.depth = 8 # QImage expects 8bpp raw = self.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 = self.export('PNG') return QImage.fromData(QByteArray(raw), 'PNG')