Pillow/Tests/test_image_array.py

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

102 lines
3.0 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
2020-01-27 14:46:52 +03:00
import pytest
from packaging.version import parse as parse_version
from PIL import Image
2020-01-27 14:46:52 +03:00
from .helper import hopper
numpy = pytest.importorskip("numpy", reason="NumPy not installed")
im = hopper().resize((128, 100))
2014-06-10 13:10:47 +04:00
def test_toarray() -> None:
2020-01-27 14:46:52 +03:00
def test(mode):
ai = numpy.array(im.convert(mode))
return ai.shape, ai.dtype.str, ai.nbytes
2020-01-27 14:46:52 +03:00
def test_with_dtype(dtype) -> None:
ai = numpy.array(im, dtype=dtype)
assert ai.dtype == dtype
# assert test("1") == ((100, 128), '|b1', 1600))
assert test("L") == ((100, 128), "|u1", 12800)
2020-01-27 14:46:52 +03:00
# FIXME: wrong?
assert test("I") == ((100, 128), Image._ENDIAN + "i4", 51200)
2020-01-27 14:46:52 +03:00
# FIXME: wrong?
assert test("F") == ((100, 128), Image._ENDIAN + "f4", 51200)
assert test("LA") == ((100, 128, 2), "|u1", 25600)
assert test("RGB") == ((100, 128, 3), "|u1", 38400)
assert test("RGBA") == ((100, 128, 4), "|u1", 51200)
assert test("RGBX") == ((100, 128, 4), "|u1", 51200)
2020-01-27 14:46:52 +03:00
test_with_dtype(numpy.float64)
test_with_dtype(numpy.uint8)
with Image.open("Tests/images/truncated_jpeg.jpg") as im_truncated:
if parse_version(numpy.__version__) >= parse_version("1.23"):
with pytest.raises(OSError):
numpy.array(im_truncated)
else:
with pytest.warns(UserWarning):
numpy.array(im_truncated)
2020-01-27 14:46:52 +03:00
def test_fromarray() -> None:
2020-01-27 14:46:52 +03:00
class Wrapper:
2021-08-12 14:50:09 +03:00
"""Class with API matching Image.fromarray"""
2020-01-27 14:46:52 +03:00
def __init__(self, img, arr_params) -> None:
2020-01-27 14:46:52 +03:00
self.img = img
self.__array_interface__ = arr_params
def tobytes(self):
return self.img.tobytes()
def test(mode):
i = im.convert(mode)
a = numpy.array(i)
2020-01-27 14:46:52 +03:00
# Make wrapper instance for image, new array interface
wrapped = Wrapper(
i,
{
"shape": a.shape,
"typestr": a.dtype.str,
"version": 3,
"data": a.data,
"strides": 1, # pretend it's non-contiguous
},
)
2020-01-27 14:46:52 +03:00
out = Image.fromarray(wrapped)
return out.mode, out.size, list(i.getdata()) == list(out.getdata())
# assert test("1") == ("1", (128, 100), True)
assert test("L") == ("L", (128, 100), True)
assert test("I") == ("I", (128, 100), True)
assert test("F") == ("F", (128, 100), True)
assert test("LA") == ("LA", (128, 100), True)
assert test("RGB") == ("RGB", (128, 100), True)
assert test("RGBA") == ("RGBA", (128, 100), True)
assert test("RGBX") == ("RGBA", (128, 100), True)
# Test mode is None with no "typestr" in the array interface
with pytest.raises(TypeError):
wrapped = Wrapper(test("L"), {"shape": (100, 128)})
Image.fromarray(wrapped)
2022-05-09 11:50:54 +03:00
def test_fromarray_palette() -> None:
2022-05-09 11:50:54 +03:00
# Arrange
i = im.convert("L")
a = numpy.array(i)
# Act
out = Image.fromarray(a, "P")
# Assert that the Python and C palettes match
assert len(out.palette.colors) == len(out.im.getpalette()) / 3