Pillow/Tests/test_image_fromqimage.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

70 lines
1.7 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
import warnings
2024-01-25 14:18:46 +03:00
from typing import Generator
2020-03-27 11:50:51 +03:00
import pytest
from PIL import Image
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=DeprecationWarning)
from PIL import ImageQt
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"))