2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2022-04-22 12:06:20 +03:00
|
|
|
import warnings
|
|
|
|
|
2020-03-27 11:50:51 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2022-04-22 12:06:20 +03:00
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("ignore", category=DeprecationWarning)
|
|
|
|
from PIL import ImageQt
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2020-03-27 12:30:00 +03:00
|
|
|
from .helper import assert_image_equal, hopper
|
2020-03-27 11:50:51 +03:00
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
pytestmark = pytest.mark.skipif(
|
|
|
|
not ImageQt.qt_is_installed, reason="Qt bindings are not installed"
|
|
|
|
)
|
2020-03-27 11:50:51 +03:00
|
|
|
|
2024-03-27 22:02:02 +03:00
|
|
|
ims: list[Image.Image] = []
|
2020-03-27 12:30:00 +03:00
|
|
|
|
2024-03-02 05:12:17 +03:00
|
|
|
|
2024-03-27 18:51:33 +03:00
|
|
|
def setup_module() -> None:
|
|
|
|
ims.append(hopper())
|
|
|
|
ims.append(Image.open("Tests/images/transparent.png"))
|
|
|
|
ims.append(Image.open("Tests/images/7x13.png"))
|
|
|
|
|
2024-03-27 18:57:33 +03:00
|
|
|
|
2024-03-02 05:12:17 +03:00
|
|
|
def teardown_module() -> None:
|
|
|
|
for im in ims:
|
|
|
|
im.close()
|
2020-03-27 11:50:51 +03:00
|
|
|
|
2020-03-27 12:30:00 +03:00
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def roundtrip(expected: Image.Image) -> None:
|
2020-03-27 11:50:51 +03:00
|
|
|
# PIL -> Qt
|
|
|
|
intermediate = expected.toqimage()
|
|
|
|
# Qt -> PIL
|
|
|
|
result = ImageQt.fromqimage(intermediate)
|
|
|
|
|
|
|
|
if intermediate.hasAlphaChannel():
|
|
|
|
assert_image_equal(result, expected.convert("RGBA"))
|
|
|
|
else:
|
|
|
|
assert_image_equal(result, expected.convert("RGB"))
|
|
|
|
|
2020-03-27 12:30:00 +03:00
|
|
|
|
2024-03-02 05:12:17 +03:00
|
|
|
def test_sanity_1() -> None:
|
|
|
|
for im in ims:
|
2020-03-27 11:50:51 +03:00
|
|
|
roundtrip(im.convert("1"))
|
|
|
|
|
2020-03-27 12:30:00 +03:00
|
|
|
|
2024-03-02 05:12:17 +03:00
|
|
|
def test_sanity_rgb() -> None:
|
|
|
|
for im in ims:
|
2020-03-27 11:50:51 +03:00
|
|
|
roundtrip(im.convert("RGB"))
|
|
|
|
|
2020-03-27 12:30:00 +03:00
|
|
|
|
2024-03-02 05:12:17 +03:00
|
|
|
def test_sanity_rgba() -> None:
|
|
|
|
for im in ims:
|
2020-03-27 11:50:51 +03:00
|
|
|
roundtrip(im.convert("RGBA"))
|
|
|
|
|
2020-03-27 12:30:00 +03:00
|
|
|
|
2024-03-02 05:12:17 +03:00
|
|
|
def test_sanity_l() -> None:
|
|
|
|
for im in ims:
|
2020-03-27 11:50:51 +03:00
|
|
|
roundtrip(im.convert("L"))
|
|
|
|
|
2020-03-27 12:30:00 +03:00
|
|
|
|
2024-03-02 05:12:17 +03:00
|
|
|
def test_sanity_p() -> None:
|
|
|
|
for im in ims:
|
2020-03-27 11:50:51 +03:00
|
|
|
roundtrip(im.convert("P"))
|