Skip to content

Commit

Permalink
Moved RGB fix inside ImageQt class
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Mar 10, 2021
1 parent 21da5b1 commit e54880c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
27 changes: 25 additions & 2 deletions Tests/test_qt_image_qapplication.py
Expand Up @@ -2,18 +2,26 @@

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:
from PIL.ImageQt import QPixmap

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
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
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
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

class Example(QWidget):
Expand Down Expand Up @@ -49,7 +57,8 @@ def test_sanity(tmp_path):

for mode in ("1", "RGB", "RGBA", "L", "P"):
# to QPixmap
data = ImageQt.toqpixmap(hopper(mode))
im = hopper(mode)
data = ImageQt.toqpixmap(im)

assert isinstance(data, QPixmap)
assert not data.isNull()
Expand All @@ -58,6 +67,20 @@ def test_sanity(tmp_path):
tempfile = str(tmp_path / f"temp_{mode}.png")
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
roundtrip(hopper(mode))

Expand Down
9 changes: 4 additions & 5 deletions src/PIL/ImageQt.py
Expand Up @@ -153,7 +153,10 @@ def _toqclass_helper(im):
for i in range(0, len(palette), 3):
colortable.append(rgb(*palette[i : i + 3]))
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
elif im.mode == "RGBA":
data = im.tobytes("raw", "BGRA")
Expand Down Expand Up @@ -206,9 +209,5 @@ def toqpixmap(im):
# im_data = _toqclass_helper(im)
# result = QPixmap(im_data["size"][0], im_data["size"][1])
# result.loadFromData(im_data["data"])
# Fix some strange bug that causes
if im.mode == "RGB":
im = im.convert("RGBA")

qimage = toqimage(im)
return QPixmap.fromImage(qimage)

0 comments on commit e54880c

Please sign in to comment.