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
|
2024-01-25 14:18:46 +03:00
|
|
|
from typing import Generator
|
2022-04-22 12:06:20 +03:00
|
|
|
|
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
|
|
|
|
2020-03-27 12:30:00 +03:00
|
|
|
|
2020-03-27 11:50:51 +03:00
|
|
|
@pytest.fixture
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_images() -> Generator[Image.Image, None, None]:
|
2020-03-27 11:50:51 +03:00
|
|
|
ims = [
|
|
|
|
hopper(),
|
|
|
|
Image.open("Tests/images/transparent.png"),
|
|
|
|
Image.open("Tests/images/7x13.png"),
|
|
|
|
]
|
|
|
|
try:
|
|
|
|
yield ims
|
|
|
|
finally:
|
2020-03-28 04:51:28 +03:00
|
|
|
for im in ims:
|
2020-03-27 11:50:51 +03:00
|
|
|
im.close()
|
|
|
|
|
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-01-25 14:18:46 +03:00
|
|
|
def test_sanity_1(test_images: Generator[Image.Image, None, None]) -> None:
|
2020-03-27 11:50:51 +03:00
|
|
|
for im in test_images:
|
|
|
|
roundtrip(im.convert("1"))
|
|
|
|
|
2020-03-27 12:30:00 +03:00
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_sanity_rgb(test_images: Generator[Image.Image, None, None]) -> None:
|
2020-03-27 11:50:51 +03:00
|
|
|
for im in test_images:
|
|
|
|
roundtrip(im.convert("RGB"))
|
|
|
|
|
2020-03-27 12:30:00 +03:00
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_sanity_rgba(test_images: Generator[Image.Image, None, None]) -> None:
|
2020-03-27 11:50:51 +03:00
|
|
|
for im in test_images:
|
|
|
|
roundtrip(im.convert("RGBA"))
|
|
|
|
|
2020-03-27 12:30:00 +03:00
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_sanity_l(test_images: Generator[Image.Image, None, None]) -> None:
|
2020-03-27 11:50:51 +03:00
|
|
|
for im in test_images:
|
|
|
|
roundtrip(im.convert("L"))
|
|
|
|
|
2020-03-27 12:30:00 +03:00
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_sanity_p(test_images: Generator[Image.Image, None, None]) -> None:
|
2020-03-27 11:50:51 +03:00
|
|
|
for im in test_images:
|
|
|
|
roundtrip(im.convert("P"))
|