2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
from pathlib import Path
|
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
import pytest
|
2020-09-01 20:16:46 +03:00
|
|
|
|
2023-04-02 00:27:22 +03:00
|
|
|
from PIL import ImageQt
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2021-02-21 14:15:56 +03:00
|
|
|
from .helper import assert_image_equal, assert_image_equal_tofile, hopper
|
2020-03-27 12:30:00 +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"
|
|
|
|
)
|
2014-09-15 22:24:56 +04:00
|
|
|
|
|
|
|
if ImageQt.qt_is_installed:
|
2017-02-19 13:04:18 +03:00
|
|
|
from PIL.ImageQt import QImage
|
2016-10-27 23:57:11 +03:00
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("RGB", "RGBA", "L", "P", "1"))
|
2024-02-20 07:41:20 +03:00
|
|
|
def test_sanity(mode: str, tmp_path: Path) -> None:
|
2022-08-23 14:41:32 +03:00
|
|
|
src = hopper(mode)
|
|
|
|
data = ImageQt.toqimage(src)
|
|
|
|
|
2024-08-26 16:49:03 +03:00
|
|
|
assert isinstance(data, QImage) # type: ignore[arg-type, misc]
|
2022-08-23 14:41:32 +03:00
|
|
|
assert not data.isNull()
|
|
|
|
|
|
|
|
# reload directly from the qimage
|
|
|
|
rt = ImageQt.fromqimage(data)
|
|
|
|
if mode in ("L", "P", "1"):
|
|
|
|
assert_image_equal(rt, src.convert("RGB"))
|
|
|
|
else:
|
|
|
|
assert_image_equal(rt, src)
|
|
|
|
|
|
|
|
if mode == "1":
|
2023-04-08 10:29:28 +03:00
|
|
|
# BW appears to not save correctly on Qt
|
2022-08-23 14:41:32 +03:00
|
|
|
# kicks out errors on console:
|
|
|
|
# libpng warning: Invalid color type/bit depth combination
|
|
|
|
# in IHDR
|
|
|
|
# libpng error: Invalid IHDR data
|
|
|
|
return
|
|
|
|
|
|
|
|
# Test saving the file
|
|
|
|
tempfile = str(tmp_path / f"temp_{mode}.png")
|
|
|
|
data.save(tempfile)
|
|
|
|
|
|
|
|
# Check that it actually worked.
|
|
|
|
assert_image_equal_tofile(src, tempfile)
|