2022-10-03 08:57:42 +03:00
|
|
|
import pytest
|
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
from PIL import Image
|
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
from .helper import hopper
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
original = hopper().resize((32, 32)).convert("I")
|
2014-04-11 11:28:06 +04:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
def verify(im1):
|
|
|
|
im2 = original.copy()
|
|
|
|
assert im1.size == im2.size
|
|
|
|
pix1 = im1.load()
|
|
|
|
pix2 = im2.load()
|
|
|
|
for y in range(im1.size[1]):
|
|
|
|
for x in range(im1.size[0]):
|
|
|
|
xy = x, y
|
|
|
|
p1 = pix1[xy]
|
|
|
|
p2 = pix2[xy]
|
2020-07-16 12:43:29 +03:00
|
|
|
assert (
|
|
|
|
p1 == p2
|
|
|
|
), f"got {repr(p1)} from mode {im1.mode} at {xy}, expected {repr(p2)}"
|
2014-07-24 04:02:57 +04:00
|
|
|
|
2014-04-11 11:28:06 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("L", "I;16", "I;16B", "I;16L", "I"))
|
|
|
|
def test_basic(tmp_path, mode):
|
2020-03-22 22:54:54 +03:00
|
|
|
# PIL 1.1 has limited support for 16-bit image data. Check that
|
|
|
|
# create/copy/transform and save works as expected.
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
im_in = original.convert(mode)
|
|
|
|
verify(im_in)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
w, h = im_in.size
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
im_out = im_in.copy()
|
|
|
|
verify(im_out) # copy
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
im_out = im_in.transform((w, h), Image.Transform.EXTENT, (0, 0, w, h))
|
|
|
|
verify(im_out) # transform
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
filename = str(tmp_path / "temp.im")
|
|
|
|
im_in.save(filename)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
with Image.open(filename) as im_out:
|
2022-04-10 21:23:31 +03:00
|
|
|
verify(im_in)
|
|
|
|
verify(im_out)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
im_out = im_in.crop((0, 0, w, h))
|
|
|
|
verify(im_out)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
im_out = Image.new(mode, (w, h), None)
|
|
|
|
im_out.paste(im_in.crop((0, 0, w // 2, h)), (0, 0))
|
|
|
|
im_out.paste(im_in.crop((w // 2, 0, w, h)), (w // 2, 0))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
verify(im_in)
|
|
|
|
verify(im_out)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
im_in = Image.new(mode, (1, 1), 1)
|
|
|
|
assert im_in.getpixel((0, 0)) == 1
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
im_in.putpixel((0, 0), 2)
|
|
|
|
assert im_in.getpixel((0, 0)) == 2
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
if mode == "L":
|
|
|
|
maximum = 255
|
|
|
|
else:
|
|
|
|
maximum = 32767
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
im_in = Image.new(mode, (1, 1), 256)
|
|
|
|
assert im_in.getpixel((0, 0)) == min(256, maximum)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
im_in.putpixel((0, 0), 512)
|
|
|
|
assert im_in.getpixel((0, 0)) == min(512, maximum)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
def test_tobytes():
|
|
|
|
def tobytes(mode):
|
|
|
|
return Image.new(mode, (1, 1), 1).tobytes()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
order = 1 if Image._ENDIAN == "<" else -1
|
2013-04-07 21:02:11 +04:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
assert tobytes("L") == b"\x01"
|
|
|
|
assert tobytes("I;16") == b"\x01\x00"
|
|
|
|
assert tobytes("I;16B") == b"\x00\x01"
|
|
|
|
assert tobytes("I") == b"\x01\x00\x00\x00"[::order]
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
def test_convert():
|
|
|
|
im = original.copy()
|
|
|
|
|
|
|
|
verify(im.convert("I;16"))
|
|
|
|
verify(im.convert("I;16").convert("L"))
|
|
|
|
verify(im.convert("I;16").convert("I"))
|
|
|
|
|
|
|
|
verify(im.convert("I;16B"))
|
|
|
|
verify(im.convert("I;16B").convert("L"))
|
|
|
|
verify(im.convert("I;16B").convert("I"))
|