Pillow/Tests/test_file_palm.py

91 lines
1.7 KiB
Python
Raw Normal View History

2014-07-01 23:18:40 +04:00
import os.path
2020-03-02 17:02:19 +03:00
import subprocess
2014-07-01 23:18:40 +04:00
import pytest
2020-03-02 17:02:19 +03:00
from PIL import Image
from .helper import (
2020-03-02 17:02:19 +03:00
IMCONVERT,
assert_image_equal,
hopper,
imagemagick_available,
skip_known_bad_test,
)
2020-03-02 17:02:19 +03:00
_roundtrip = imagemagick_available()
2014-07-01 23:18:40 +04:00
2014-08-28 15:44:19 +04:00
2020-03-02 17:02:19 +03:00
def helper_save_as_palm(tmp_path, mode):
# 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
2020-03-02 17:02:19 +03:00
def open_with_imagemagick(tmp_path, f):
if not imagemagick_available():
raise OSError()
2020-03-02 17:02:19 +03:00
outfile = str(tmp_path / "temp.png")
rc = subprocess.call(
[IMCONVERT, f, outfile], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
)
if rc:
raise OSError
return Image.open(outfile)
2014-07-01 23:53:30 +04:00
2020-03-02 17:02:19 +03:00
def roundtrip(tmp_path, mode):
if not _roundtrip:
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)
converted = open_with_imagemagick(tmp_path, outfile)
assert_image_equal(converted, im)
2014-08-28 15:44:19 +04:00
2019-04-15 11:07:15 +03:00
2020-03-02 17:02:19 +03:00
def test_monochrome(tmp_path):
# 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
def test_p_mode(tmp_path):
# Arrange
mode = "P"
# Act / Assert
helper_save_as_palm(tmp_path, mode)
skip_known_bad_test("Palm P image is wrong")
roundtrip(tmp_path, mode)
def test_l_ioerror(tmp_path):
# Arrange
mode = "L"
# Act / Assert
with pytest.raises(IOError):
helper_save_as_palm(tmp_path, mode)
def test_rgb_ioerror(tmp_path):
# Arrange
mode = "RGB"
# Act / Assert
with pytest.raises(IOError):
helper_save_as_palm(tmp_path, mode)