Pillow/Tests/test_mode_i16.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

99 lines
2.4 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
from pathlib import Path
2022-10-03 08:57:42 +03:00
import pytest
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")
2024-01-31 13:55:32 +03:00
def verify(im1: Image.Image) -> None:
2020-03-22 22:54:54 +03:00
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)}"
2022-10-03 08:57:42 +03:00
@pytest.mark.parametrize("mode", ("L", "I;16", "I;16B", "I;16L", "I"))
2024-01-31 13:55:32 +03:00
def test_basic(tmp_path: Path, mode: str) -> None:
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.
2022-10-03 08:57:42 +03:00
im_in = original.convert(mode)
verify(im_in)
2022-10-03 08:57:42 +03:00
w, h = im_in.size
2022-10-03 08:57:42 +03:00
im_out = im_in.copy()
verify(im_out) # copy
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
2022-10-03 08:57:42 +03:00
filename = str(tmp_path / "temp.im")
im_in.save(filename)
2022-10-03 08:57:42 +03:00
with Image.open(filename) as im_out:
verify(im_in)
verify(im_out)
2022-10-03 08:57:42 +03:00
im_out = im_in.crop((0, 0, w, h))
verify(im_out)
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))
2022-10-03 08:57:42 +03:00
verify(im_in)
verify(im_out)
2022-10-03 08:57:42 +03:00
im_in = Image.new(mode, (1, 1), 1)
assert im_in.getpixel((0, 0)) == 1
2022-10-03 08:57:42 +03:00
im_in.putpixel((0, 0), 2)
assert im_in.getpixel((0, 0)) == 2
2022-10-03 08:57:42 +03:00
if mode == "L":
maximum = 255
else:
maximum = 32767
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)
2022-10-03 08:57:42 +03:00
im_in.putpixel((0, 0), 512)
assert im_in.getpixel((0, 0)) == min(512, maximum)
def test_tobytes() -> None:
2024-03-02 05:12:17 +03:00
def tobytes(mode: str) -> bytes:
2020-03-22 22:54:54 +03:00
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]
def test_convert() -> None:
2020-03-22 22:54:54 +03:00
im = original.copy()
2022-12-28 09:16:44 +03:00
for mode in ("I;16", "I;16B", "I;16N"):
verify(im.convert(mode))
verify(im.convert(mode).convert("L"))
verify(im.convert(mode).convert("I"))