2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2022-02-21 05:49:01 +03:00
|
|
|
import warnings
|
|
|
|
|
2020-03-27 11:50:51 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2023-04-02 00:27:22 +03:00
|
|
|
from PIL import ImageQt
|
2022-04-22 12:06:20 +03:00
|
|
|
|
2023-04-02 00:27:22 +03:00
|
|
|
from .helper import assert_image_similar, hopper
|
2022-04-22 12:06:20 +03:00
|
|
|
|
2021-02-11 10:19:05 +03:00
|
|
|
pytestmark = pytest.mark.skipif(
|
|
|
|
not ImageQt.qt_is_installed, reason="Qt bindings are not installed"
|
|
|
|
)
|
|
|
|
|
2014-09-15 22:24:56 +04:00
|
|
|
if ImageQt.qt_is_installed:
|
2015-06-19 08:35:56 +03:00
|
|
|
from PIL.ImageQt import qRgba
|
2014-09-15 22:24:56 +04:00
|
|
|
|
2019-06-13 18:54:46 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_rgb() -> None:
|
2020-03-27 12:30:00 +03:00
|
|
|
# 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
|
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)
|
|
|
|
|
2024-02-20 07:41:20 +03:00
|
|
|
def checkrgb(r: int, g: int, b: int) -> None:
|
2020-03-27 12:30:00 +03:00
|
|
|
val = ImageQt.rgb(r, g, b)
|
2022-03-04 08:42:24 +03:00
|
|
|
val = val % 2**24 # drop the alpha
|
2020-03-27 12:30:00 +03:00
|
|
|
assert val >> 16 == r
|
2022-03-04 08:42:24 +03:00
|
|
|
assert ((val >> 8) % 2**8) == g
|
|
|
|
assert val % 2**8 == b
|
2020-03-27 12:30:00 +03:00
|
|
|
|
|
|
|
checkrgb(0, 0, 0)
|
|
|
|
checkrgb(255, 0, 0)
|
|
|
|
checkrgb(0, 255, 0)
|
|
|
|
checkrgb(0, 0, 255)
|
|
|
|
|
|
|
|
|
2024-06-21 17:39:37 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("1", "RGB", "RGBA", "L", "P", "I;16"))
|
|
|
|
def test_image(mode: str) -> None:
|
|
|
|
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)
|
2021-02-11 10:19:05 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_closed_file() -> None:
|
2022-02-21 05:49:01 +03:00
|
|
|
with warnings.catch_warnings():
|
2024-10-26 08:15:46 +03:00
|
|
|
warnings.simplefilter("error")
|
|
|
|
|
2021-02-11 10:19:05 +03:00
|
|
|
ImageQt.ImageQt("Tests/images/hopper.gif")
|