2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2014-07-01 23:18:40 +04:00
|
|
|
import os.path
|
2020-03-02 17:02:19 +03:00
|
|
|
import subprocess
|
2024-01-31 12:12:58 +03:00
|
|
|
from pathlib import Path
|
2014-07-01 23:18:40 +04:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
from PIL import Image
|
2020-02-22 16:06:21 +03:00
|
|
|
|
2021-03-21 23:48:13 +03:00
|
|
|
from .helper import assert_image_equal, hopper, magick_command
|
2014-07-01 23:18:40 +04:00
|
|
|
|
2014-08-28 15:44:19 +04:00
|
|
|
|
2024-02-12 13:06:17 +03:00
|
|
|
def helper_save_as_palm(tmp_path: Path, mode: str) -> None:
|
2020-03-02 17:02:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = hopper(mode)
|
|
|
|
outfile = str(tmp_path / ("temp_" + mode + ".palm"))
|
2014-07-01 23:18:40 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
# Act
|
|
|
|
im.save(outfile)
|
2014-07-01 23:18:40 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
# Assert
|
|
|
|
assert os.path.isfile(outfile)
|
|
|
|
assert os.path.getsize(outfile) > 0
|
2014-07-01 23:18:40 +04:00
|
|
|
|
2014-08-28 15:44:19 +04:00
|
|
|
|
2024-02-12 13:06:17 +03:00
|
|
|
def open_with_magick(magick: list[str], tmp_path: Path, f: str) -> Image.Image:
|
2020-03-02 17:02:19 +03:00
|
|
|
outfile = str(tmp_path / "temp.png")
|
|
|
|
rc = subprocess.call(
|
2021-03-21 23:48:13 +03:00
|
|
|
magick + [f, outfile], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
|
2020-03-02 17:02:19 +03:00
|
|
|
)
|
2023-10-19 12:34:28 +03:00
|
|
|
assert not rc
|
2020-03-02 17:02:19 +03:00
|
|
|
return Image.open(outfile)
|
2014-07-05 21:25:16 +04:00
|
|
|
|
2014-07-01 23:53:30 +04:00
|
|
|
|
2024-02-12 13:06:17 +03:00
|
|
|
def roundtrip(tmp_path: Path, mode: str) -> None:
|
2021-03-21 23:48:13 +03:00
|
|
|
magick = magick_command()
|
|
|
|
if not magick:
|
2020-03-02 17:02:19 +03:00
|
|
|
return
|
2014-07-01 23:53:30 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
im = hopper(mode)
|
|
|
|
outfile = str(tmp_path / "temp.palm")
|
2014-07-01 23:53:30 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
im.save(outfile)
|
2021-03-21 23:48:13 +03:00
|
|
|
converted = open_with_magick(magick, tmp_path, outfile)
|
2020-03-02 17:02:19 +03:00
|
|
|
assert_image_equal(converted, im)
|
2014-08-28 15:44:19 +04:00
|
|
|
|
2019-04-15 11:07:15 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_monochrome(tmp_path: Path) -> None:
|
2020-03-02 17:02:19 +03:00
|
|
|
# Arrange
|
|
|
|
mode = "1"
|
2019-04-15 11:07:15 +03:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
# Act / Assert
|
|
|
|
helper_save_as_palm(tmp_path, mode)
|
|
|
|
roundtrip(tmp_path, mode)
|
2014-07-01 23:53:30 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
|
2020-06-14 21:08:27 +03:00
|
|
|
@pytest.mark.xfail(reason="Palm P image is wrong")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_p_mode(tmp_path: Path) -> None:
|
2020-03-02 17:02:19 +03:00
|
|
|
# Arrange
|
|
|
|
mode = "P"
|
|
|
|
|
|
|
|
# Act / Assert
|
|
|
|
helper_save_as_palm(tmp_path, mode)
|
|
|
|
roundtrip(tmp_path, mode)
|
|
|
|
|
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("L", "RGB"))
|
2024-02-12 13:06:17 +03:00
|
|
|
def test_oserror(tmp_path: Path, mode: str) -> None:
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
2020-03-02 17:02:19 +03:00
|
|
|
helper_save_as_palm(tmp_path, mode)
|