Pillow/Tests/test_qt_image_qapplication.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
3.5 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
from pathlib import Path
2024-08-26 16:49:03 +03:00
from typing import TYPE_CHECKING, Union
2021-01-01 03:33:20 +03:00
import pytest
2024-02-20 07:41:20 +03:00
from PIL import Image, ImageQt
2021-01-01 03:33:20 +03:00
2023-01-28 06:15:51 +03:00
from .helper import assert_image_equal_tofile, assert_image_similar, hopper
2021-01-01 03:33:20 +03:00
2024-08-26 16:49:03 +03:00
if TYPE_CHECKING:
import PyQt6
import PySide6
QApplication = Union[PyQt6.QtWidgets.QApplication, PySide6.QtWidgets.QApplication]
QHBoxLayout = Union[PyQt6.QtWidgets.QHBoxLayout, PySide6.QtWidgets.QHBoxLayout]
QImage = Union[PyQt6.QtGui.QImage, PySide6.QtGui.QImage]
QLabel = Union[PyQt6.QtWidgets.QLabel, PySide6.QtWidgets.QLabel]
QPainter = Union[PyQt6.QtGui.QPainter, PySide6.QtGui.QPainter]
QPixmap = Union[PyQt6.QtGui.QPixmap, PySide6.QtGui.QPixmap]
QPoint = Union[PyQt6.QtCore.QPoint, PySide6.QtCore.QPoint]
QRegion = Union[PyQt6.QtGui.QRegion, PySide6.QtGui.QRegion]
QWidget = Union[PyQt6.QtWidgets.QWidget, PySide6.QtWidgets.QWidget]
2021-01-01 03:33:20 +03:00
if ImageQt.qt_is_installed:
from PIL.ImageQt import QPixmap
2021-02-10 13:12:30 +03:00
if ImageQt.qt_version == "6":
2021-03-10 05:17:19 +03:00
from PyQt6.QtCore import QPoint
from PyQt6.QtGui import QImage, QPainter, QRegion
2021-02-10 13:12:30 +03:00
from PyQt6.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
elif ImageQt.qt_version == "side6":
2021-03-10 05:17:19 +03:00
from PySide6.QtCore import QPoint
from PySide6.QtGui import QImage, QPainter, QRegion
2020-12-28 13:58:08 +03:00
from PySide6.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
2021-01-01 03:33:20 +03:00
2024-08-26 16:49:03 +03:00
class Example(QWidget): # type: ignore[misc]
def __init__(self) -> None:
2021-01-01 03:33:20 +03:00
super().__init__()
img = hopper().resize((1000, 1000))
qimage = ImageQt.ImageQt(img)
2024-08-26 16:49:03 +03:00
pixmap1 = getattr(ImageQt.QPixmap, "fromImage")(qimage)
2021-01-01 03:33:20 +03:00
2024-08-26 16:49:03 +03:00
# hbox
QHBoxLayout(self) # type: ignore[operator]
2021-01-01 03:33:20 +03:00
2024-08-26 16:49:03 +03:00
lbl = QLabel(self) # type: ignore[operator]
2021-01-01 03:33:20 +03:00
# Segfault in the problem
lbl.setPixmap(pixmap1.copy())
2024-02-20 07:41:20 +03:00
def roundtrip(expected: Image.Image) -> None:
2021-01-01 03:33:20 +03:00
result = ImageQt.fromqpixmap(ImageQt.toqpixmap(expected))
# Qt saves all pixmaps as rgb
2023-02-24 00:39:48 +03:00
assert_image_similar(result, expected.convert("RGB"), 1)
2021-01-01 03:33:20 +03:00
@pytest.mark.skipif(not ImageQt.qt_is_installed, reason="Qt bindings are not installed")
def test_sanity(tmp_path: Path) -> None:
2021-01-01 03:33:20 +03:00
# Segfault test
2024-08-26 16:49:03 +03:00
app: QApplication | None = QApplication([]) # type: ignore[operator]
2021-01-01 03:33:20 +03:00
ex = Example()
assert app # Silence warning
assert ex # Silence warning
for mode in ("1", "RGB", "RGBA", "L", "P"):
# to QPixmap
2021-03-10 05:17:19 +03:00
im = hopper(mode)
data = ImageQt.toqpixmap(im)
2021-01-01 03:33:20 +03:00
2024-08-26 16:49:03 +03:00
assert data.__class__.__name__ == "QPixmap"
2021-01-01 03:33:20 +03:00
assert not data.isNull()
# Test saving the file
tempfile = str(tmp_path / f"temp_{mode}.png")
data.save(tempfile)
2021-03-10 05:17:19 +03:00
# Render the image
2024-08-26 16:49:03 +03:00
imageqt = ImageQt.ImageQt(im)
data = getattr(QPixmap, "fromImage")(imageqt)
qt_format = getattr(QImage, "Format") if ImageQt.qt_version == "6" else QImage
qimage = QImage(128, 128, getattr(qt_format, "Format_ARGB32")) # type: ignore[operator]
painter = QPainter(qimage) # type: ignore[operator]
image_label = QLabel() # type: ignore[operator]
2021-03-10 05:17:19 +03:00
image_label.setPixmap(data)
2024-08-26 16:49:03 +03:00
image_label.render(painter, QPoint(0, 0), QRegion(0, 0, 128, 128)) # type: ignore[operator]
2021-03-10 05:17:19 +03:00
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)
2021-01-01 03:33:20 +03:00
# from QPixmap
roundtrip(hopper(mode))
app.quit()
app = None