Pillow/Tests/test_mode_i16.py

107 lines
2.5 KiB
Python
Raw Permalink Normal View History

from PIL import Image
2020-03-22 22:54:54 +03:00
from .helper import hopper
2020-03-22 22:54:54 +03:00
original = hopper().resize((32, 32)).convert("I")
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]
assert (
p1 == p2
), f"got {repr(p1)} from mode {im1.mode} at {xy}, expected {repr(p2)}"
2020-03-22 22:54:54 +03:00
def test_basic(tmp_path):
# PIL 1.1 has limited support for 16-bit image data. Check that
# create/copy/transform and save works as expected.
2020-03-22 22:54:54 +03:00
def basic(mode):
2020-03-22 22:54:54 +03:00
imIn = original.convert(mode)
verify(imIn)
2020-03-22 22:54:54 +03:00
w, h = imIn.size
2020-03-22 22:54:54 +03:00
imOut = imIn.copy()
verify(imOut) # copy
2020-03-22 22:54:54 +03:00
imOut = imIn.transform((w, h), Image.EXTENT, (0, 0, w, h))
verify(imOut) # transform
2020-03-22 22:54:54 +03:00
filename = str(tmp_path / "temp.im")
imIn.save(filename)
2020-03-22 22:54:54 +03:00
with Image.open(filename) as imOut:
2020-03-22 22:54:54 +03:00
verify(imIn)
verify(imOut)
2020-03-22 22:54:54 +03:00
imOut = imIn.crop((0, 0, w, h))
verify(imOut)
2020-03-22 22:54:54 +03:00
imOut = Image.new(mode, (w, h), None)
imOut.paste(imIn.crop((0, 0, w // 2, h)), (0, 0))
imOut.paste(imIn.crop((w // 2, 0, w, h)), (w // 2, 0))
2020-03-22 22:54:54 +03:00
verify(imIn)
verify(imOut)
2020-03-22 22:54:54 +03:00
imIn = Image.new(mode, (1, 1), 1)
assert imIn.getpixel((0, 0)) == 1
2020-03-22 22:54:54 +03:00
imIn.putpixel((0, 0), 2)
assert imIn.getpixel((0, 0)) == 2
2020-03-22 22:54:54 +03:00
if mode == "L":
maximum = 255
else:
maximum = 32767
2020-03-22 22:54:54 +03:00
imIn = Image.new(mode, (1, 1), 256)
assert imIn.getpixel((0, 0)) == min(256, maximum)
2020-03-22 22:54:54 +03:00
imIn.putpixel((0, 0), 512)
assert imIn.getpixel((0, 0)) == min(512, maximum)
2020-03-22 22:54:54 +03:00
basic("L")
2020-03-22 22:54:54 +03:00
basic("I;16")
basic("I;16B")
basic("I;16L")
2020-03-22 22:54:54 +03:00
basic("I")
2020-03-22 22:54:54 +03:00
def test_tobytes():
def tobytes(mode):
return Image.new(mode, (1, 1), 1).tobytes()
2020-03-22 22:54:54 +03:00
order = 1 if Image._ENDIAN == "<" else -1
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]
2020-03-22 22:54:54 +03:00
def test_convert():
2020-03-22 22:54:54 +03:00
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"))