Moved RGB fix inside ImageQt class

This commit is contained in:
Andrew Murray 2021-03-10 13:17:19 +11:00
parent 21da5b1ed8
commit e54880c652
2 changed files with 29 additions and 7 deletions

View File

@ -2,18 +2,26 @@ import pytest
from PIL import ImageQt from PIL import ImageQt
from .helper import assert_image_equal, hopper from .helper import assert_image_equal, assert_image_equal_tofile, hopper
if ImageQt.qt_is_installed: if ImageQt.qt_is_installed:
from PIL.ImageQt import QPixmap from PIL.ImageQt import QPixmap
if ImageQt.qt_version == "6": if ImageQt.qt_version == "6":
from PyQt6.QtCore import QPoint
from PyQt6.QtGui import QImage, QPainter, QRegion
from PyQt6.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget from PyQt6.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
elif ImageQt.qt_version == "side6": elif ImageQt.qt_version == "side6":
from PySide6.QtCore import QPoint
from PySide6.QtGui import QImage, QPainter, QRegion
from PySide6.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget from PySide6.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
elif ImageQt.qt_version == "5": elif ImageQt.qt_version == "5":
from PyQt5.QtCore import QPoint
from PyQt5.QtGui import QImage, QPainter, QRegion
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget from PyQt5.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
elif ImageQt.qt_version == "side2": elif ImageQt.qt_version == "side2":
from PySide2.QtCore import QPoint
from PySide2.QtGui import QImage, QPainter, QRegion
from PySide2.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget from PySide2.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
class Example(QWidget): class Example(QWidget):
@ -49,7 +57,8 @@ def test_sanity(tmp_path):
for mode in ("1", "RGB", "RGBA", "L", "P"): for mode in ("1", "RGB", "RGBA", "L", "P"):
# to QPixmap # to QPixmap
data = ImageQt.toqpixmap(hopper(mode)) im = hopper(mode)
data = ImageQt.toqpixmap(im)
assert isinstance(data, QPixmap) assert isinstance(data, QPixmap)
assert not data.isNull() assert not data.isNull()
@ -58,6 +67,20 @@ def test_sanity(tmp_path):
tempfile = str(tmp_path / f"temp_{mode}.png") tempfile = str(tmp_path / f"temp_{mode}.png")
data.save(tempfile) data.save(tempfile)
# Render the image
qimage = ImageQt.ImageQt(im)
data = QPixmap.fromImage(qimage)
qt_format = QImage.Format if ImageQt.qt_version == "6" else QImage
qimage = QImage(128, 128, qt_format.Format_ARGB32)
painter = QPainter(qimage)
image_label = QLabel()
image_label.setPixmap(data)
image_label.render(painter, QPoint(0, 0), QRegion(0, 0, 128, 128))
painter.end()
rendered_tempfile = str(tmp_path / f"temp_rendered_{mode}.png")
qimage.save(rendered_tempfile)
assert_image_equal_tofile(im.convert("RGBA"), rendered_tempfile)
# from QPixmap # from QPixmap
roundtrip(hopper(mode)) roundtrip(hopper(mode))

View File

@ -153,7 +153,10 @@ def _toqclass_helper(im):
for i in range(0, len(palette), 3): for i in range(0, len(palette), 3):
colortable.append(rgb(*palette[i : i + 3])) colortable.append(rgb(*palette[i : i + 3]))
elif im.mode == "RGB": elif im.mode == "RGB":
data = im.tobytes("raw", "BGRX") # Populate the 4th channel with 255
im = im.convert("RGBA")
data = im.tobytes("raw", "BGRA")
format = qt_format.Format_RGB32 format = qt_format.Format_RGB32
elif im.mode == "RGBA": elif im.mode == "RGBA":
data = im.tobytes("raw", "BGRA") data = im.tobytes("raw", "BGRA")
@ -206,9 +209,5 @@ def toqpixmap(im):
# im_data = _toqclass_helper(im) # im_data = _toqclass_helper(im)
# result = QPixmap(im_data["size"][0], im_data["size"][1]) # result = QPixmap(im_data["size"][0], im_data["size"][1])
# result.loadFromData(im_data["data"]) # result.loadFromData(im_data["data"])
# Fix some strange bug that causes
if im.mode == "RGB":
im = im.convert("RGBA")
qimage = toqimage(im) qimage = toqimage(im)
return QPixmap.fromImage(qimage) return QPixmap.fromImage(qimage)