Pillow

Форк
0
/
test_qt_image_qapplication.py 
100 строк · 3.5 Кб
1
from __future__ import annotations
2

3
from pathlib import Path
4
from typing import TYPE_CHECKING, Union
5

6
import pytest
7

8
from PIL import Image, ImageQt
9

10
from .helper import assert_image_equal_tofile, assert_image_similar, hopper
11

12
if TYPE_CHECKING:
13
    import PyQt6
14
    import PySide6
15

16
    QApplication = Union[PyQt6.QtWidgets.QApplication, PySide6.QtWidgets.QApplication]
17
    QHBoxLayout = Union[PyQt6.QtWidgets.QHBoxLayout, PySide6.QtWidgets.QHBoxLayout]
18
    QImage = Union[PyQt6.QtGui.QImage, PySide6.QtGui.QImage]
19
    QLabel = Union[PyQt6.QtWidgets.QLabel, PySide6.QtWidgets.QLabel]
20
    QPainter = Union[PyQt6.QtGui.QPainter, PySide6.QtGui.QPainter]
21
    QPixmap = Union[PyQt6.QtGui.QPixmap, PySide6.QtGui.QPixmap]
22
    QPoint = Union[PyQt6.QtCore.QPoint, PySide6.QtCore.QPoint]
23
    QRegion = Union[PyQt6.QtGui.QRegion, PySide6.QtGui.QRegion]
24
    QWidget = Union[PyQt6.QtWidgets.QWidget, PySide6.QtWidgets.QWidget]
25

26
if ImageQt.qt_is_installed:
27
    from PIL.ImageQt import QPixmap
28

29
    if ImageQt.qt_version == "6":
30
        from PyQt6.QtCore import QPoint
31
        from PyQt6.QtGui import QImage, QPainter, QRegion
32
        from PyQt6.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
33
    elif ImageQt.qt_version == "side6":
34
        from PySide6.QtCore import QPoint
35
        from PySide6.QtGui import QImage, QPainter, QRegion
36
        from PySide6.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
37

38
    class Example(QWidget):  # type: ignore[misc]
39
        def __init__(self) -> None:
40
            super().__init__()
41

42
            img = hopper().resize((1000, 1000))
43

44
            qimage = ImageQt.ImageQt(img)
45

46
            pixmap1 = getattr(ImageQt.QPixmap, "fromImage")(qimage)
47

48
            # hbox
49
            QHBoxLayout(self)  # type: ignore[operator]
50

51
            lbl = QLabel(self)  # type: ignore[operator]
52
            # Segfault in the problem
53
            lbl.setPixmap(pixmap1.copy())
54

55

56
def roundtrip(expected: Image.Image) -> None:
57
    result = ImageQt.fromqpixmap(ImageQt.toqpixmap(expected))
58
    # Qt saves all pixmaps as rgb
59
    assert_image_similar(result, expected.convert("RGB"), 1)
60

61

62
@pytest.mark.skipif(not ImageQt.qt_is_installed, reason="Qt bindings are not installed")
63
def test_sanity(tmp_path: Path) -> None:
64
    # Segfault test
65
    app: QApplication | None = QApplication([])  # type: ignore[operator]
66
    ex = Example()
67
    assert app  # Silence warning
68
    assert ex  # Silence warning
69

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

75
        assert data.__class__.__name__ == "QPixmap"
76
        assert not data.isNull()
77

78
        # Test saving the file
79
        tempfile = str(tmp_path / f"temp_{mode}.png")
80
        data.save(tempfile)
81

82
        # Render the image
83
        imageqt = ImageQt.ImageQt(im)
84
        data = getattr(QPixmap, "fromImage")(imageqt)
85
        qt_format = getattr(QImage, "Format") if ImageQt.qt_version == "6" else QImage
86
        qimage = QImage(128, 128, getattr(qt_format, "Format_ARGB32"))  # type: ignore[operator]
87
        painter = QPainter(qimage)  # type: ignore[operator]
88
        image_label = QLabel()  # type: ignore[operator]
89
        image_label.setPixmap(data)
90
        image_label.render(painter, QPoint(0, 0), QRegion(0, 0, 128, 128))  # type: ignore[operator]
91
        painter.end()
92
        rendered_tempfile = str(tmp_path / f"temp_rendered_{mode}.png")
93
        qimage.save(rendered_tempfile)
94
        assert_image_equal_tofile(im.convert("RGBA"), rendered_tempfile)
95

96
        # from QPixmap
97
        roundtrip(hopper(mode))
98

99
    app.quit()
100
    app = None
101

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.