Pillow/Tests/test_imageqt.py

63 lines
1.7 KiB
Python
Raw Normal View History

import warnings
2020-03-27 11:50:51 +03:00
import pytest
from PIL import ImageQt
2021-12-14 02:13:09 +03:00
from .helper import assert_image_similar, hopper
pytestmark = pytest.mark.skipif(
not ImageQt.qt_is_installed, reason="Qt bindings are not installed"
)
if ImageQt.qt_is_installed:
2015-06-19 08:35:56 +03:00
from PIL.ImageQt import qRgba
2019-06-13 18:54:46 +03:00
2020-03-27 12:30:00 +03:00
def test_rgb():
# from https://doc.qt.io/archives/qt-4.8/qcolor.html
# typedef QRgb
# An ARGB quadruplet on the format #AARRGGBB,
# equivalent to an unsigned int.
2021-02-10 13:12:30 +03:00
if ImageQt.qt_version == "6":
from PyQt6.QtGui import qRgb
elif ImageQt.qt_version == "side6":
2020-12-28 13:58:08 +03:00
from PySide6.QtGui import qRgb
elif ImageQt.qt_version == "5":
2020-03-27 12:30:00 +03:00
from PyQt5.QtGui import qRgb
elif ImageQt.qt_version == "side2":
from PySide2.QtGui import qRgb
2015-06-19 08:36:23 +03:00
2020-03-27 12:30:00 +03:00
assert qRgb(0, 0, 0) == qRgba(0, 0, 0, 255)
def checkrgb(r, g, b):
val = ImageQt.rgb(r, g, b)
val = val % 2 ** 24 # drop the alpha
assert val >> 16 == r
assert ((val >> 8) % 2 ** 8) == g
assert val % 2 ** 8 == b
checkrgb(0, 0, 0)
checkrgb(255, 0, 0)
checkrgb(0, 255, 0)
checkrgb(0, 0, 255)
def test_image():
2021-12-14 02:13:09 +03:00
modes = ["1", "RGB", "RGBA", "L", "P"]
qt_format = ImageQt.QImage.Format if ImageQt.qt_version == "6" else ImageQt.QImage
if hasattr(qt_format, "Format_Grayscale16"): # Qt 5.13+
2021-12-14 02:13:09 +03:00
modes.append("I;16")
for mode in modes:
im = hopper(mode)
roundtripped_im = ImageQt.fromqimage(ImageQt.ImageQt(im))
if mode not in ("RGB", "RGBA"):
im = im.convert("RGB")
assert_image_similar(roundtripped_im, im, 1)
def test_closed_file():
with warnings.catch_warnings():
ImageQt.ImageQt("Tests/images/hopper.gif")