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-02-22 16:06:21 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2015-07-03 08:03:25 +03:00
|
|
|
from PIL import Image, SgiImagePlugin
|
2014-07-16 22:28:17 +04:00
|
|
|
|
2021-02-21 14:15:56 +03:00
|
|
|
from .helper import (
|
|
|
|
assert_image_equal,
|
|
|
|
assert_image_equal_tofile,
|
|
|
|
assert_image_similar,
|
|
|
|
hopper,
|
|
|
|
)
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2014-07-16 22:28:17 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_rgb() -> None:
|
2020-02-25 12:57:27 +03:00
|
|
|
# Created with ImageMagick then renamed:
|
|
|
|
# convert hopper.ppm -compress None sgi:hopper.rgb
|
|
|
|
test_file = "Tests/images/hopper.rgb"
|
2014-07-16 22:28:17 +04:00
|
|
|
|
2020-02-25 12:57:27 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
assert_image_equal(im, hopper())
|
|
|
|
assert im.get_format_mimetype() == "image/rgb"
|
2014-07-16 22:28:17 +04:00
|
|
|
|
2017-07-29 00:12:46 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_rgb16() -> None:
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(hopper(), "Tests/images/hopper16.rgb")
|
2014-07-16 22:28:17 +04:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_l() -> None:
|
2020-02-25 12:57:27 +03:00
|
|
|
# Created with ImageMagick
|
|
|
|
# convert hopper.ppm -monochrome -compress None sgi:hopper.bw
|
|
|
|
test_file = "Tests/images/hopper.bw"
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2020-02-25 12:57:27 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
assert_image_similar(im, hopper("L"), 2)
|
|
|
|
assert im.get_format_mimetype() == "image/sgi"
|
2016-12-31 01:20:46 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_rgba() -> None:
|
2020-02-25 12:57:27 +03:00
|
|
|
# Created with ImageMagick:
|
|
|
|
# convert transparent.png -compress None transparent.sgi
|
|
|
|
test_file = "Tests/images/transparent.sgi"
|
2017-07-29 00:12:46 +03:00
|
|
|
|
2020-02-25 12:57:27 +03:00
|
|
|
with Image.open(test_file) as im:
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/transparent.png")
|
2020-02-25 12:57:27 +03:00
|
|
|
assert im.get_format_mimetype() == "image/sgi"
|
2017-07-29 00:12:46 +03:00
|
|
|
|
2014-07-16 22:28:17 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_rle() -> None:
|
2020-02-25 12:57:27 +03:00
|
|
|
# Created with ImageMagick:
|
|
|
|
# convert hopper.ppm hopper.sgi
|
|
|
|
test_file = "Tests/images/hopper.sgi"
|
2015-07-03 09:22:56 +03:00
|
|
|
|
2020-02-25 12:57:27 +03:00
|
|
|
with Image.open(test_file) as im:
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/hopper.rgb")
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2016-12-31 01:31:35 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_rle16() -> None:
|
2020-02-25 12:57:27 +03:00
|
|
|
test_file = "Tests/images/tv16.sgi"
|
2016-12-31 01:31:35 +03:00
|
|
|
|
2020-02-25 12:57:27 +03:00
|
|
|
with Image.open(test_file) as im:
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/tv.rgb")
|
2017-08-07 14:57:59 +03:00
|
|
|
|
2017-09-29 13:39:43 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_invalid_file() -> None:
|
2020-02-25 12:57:27 +03:00
|
|
|
invalid_file = "Tests/images/flower.jpg"
|
2017-09-29 13:39:43 +03:00
|
|
|
|
2020-02-25 12:57:27 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
SgiImagePlugin.SgiImageFile(invalid_file)
|
2018-01-27 09:07:24 +03:00
|
|
|
|
2017-08-07 12:21:54 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_write(tmp_path: Path) -> None:
|
|
|
|
def roundtrip(img) -> None:
|
2020-02-25 12:57:27 +03:00
|
|
|
out = str(tmp_path / "temp.sgi")
|
|
|
|
img.save(out, format="sgi")
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(img, out)
|
2020-02-25 12:57:27 +03:00
|
|
|
|
2021-07-30 13:29:07 +03:00
|
|
|
out = str(tmp_path / "fp.sgi")
|
|
|
|
with open(out, "wb") as fp:
|
|
|
|
img.save(fp)
|
|
|
|
assert_image_equal_tofile(img, out)
|
|
|
|
|
|
|
|
assert not fp.closed
|
|
|
|
|
2020-02-25 12:57:27 +03:00
|
|
|
for mode in ("L", "RGB", "RGBA"):
|
|
|
|
roundtrip(hopper(mode))
|
|
|
|
|
|
|
|
# Test 1 dimension for an L mode image
|
|
|
|
roundtrip(Image.new("L", (10, 1)))
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_write16(tmp_path: Path) -> None:
|
2020-02-25 12:57:27 +03:00
|
|
|
test_file = "Tests/images/hopper16.rgb"
|
|
|
|
|
|
|
|
with Image.open(test_file) as im:
|
|
|
|
out = str(tmp_path / "temp.sgi")
|
|
|
|
im.save(out, format="sgi", bpc=2)
|
|
|
|
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, out)
|
2020-02-25 12:57:27 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_unsupported_mode(tmp_path: Path) -> None:
|
2020-02-25 12:57:27 +03:00
|
|
|
im = hopper("LA")
|
|
|
|
out = str(tmp_path / "temp.sgi")
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.save(out, format="sgi")
|