mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-10 08:12:33 +03:00
move getdata/putdata tests to same file
This commit is contained in:
parent
dc3751599f
commit
9e3209e264
|
@ -30,7 +30,6 @@ from .helper import (
|
||||||
assert_image_similar_tofile,
|
assert_image_similar_tofile,
|
||||||
assert_not_all_same,
|
assert_not_all_same,
|
||||||
hopper,
|
hopper,
|
||||||
is_big_endian,
|
|
||||||
is_win32,
|
is_win32,
|
||||||
mark_if_feature_version,
|
mark_if_feature_version,
|
||||||
skip_unless_feature,
|
skip_unless_feature,
|
||||||
|
@ -1139,15 +1138,6 @@ class TestImageBytes:
|
||||||
reloaded.frombytes(source_bytes)
|
reloaded.frombytes(source_bytes)
|
||||||
assert reloaded.tobytes() == source_bytes
|
assert reloaded.tobytes() == source_bytes
|
||||||
|
|
||||||
@pytest.mark.parametrize("mode", Image.MODES + ["BGR;15", "BGR;16", "BGR;24"])
|
|
||||||
def test_getdata_putdata(self, mode: str) -> None:
|
|
||||||
if is_big_endian() and mode == "BGR;15":
|
|
||||||
pytest.xfail("Known failure of BGR;15 on big-endian")
|
|
||||||
im = hopper(mode)
|
|
||||||
reloaded = helper_image_new(mode, im.size)
|
|
||||||
reloaded.putdata(im.getdata())
|
|
||||||
assert_image_equal(im, reloaded)
|
|
||||||
|
|
||||||
|
|
||||||
class MockEncoder(ImageFile.PyEncoder):
|
class MockEncoder(ImageFile.PyEncoder):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
from .helper import hopper
|
|
||||||
|
|
||||||
|
|
||||||
def test_sanity() -> None:
|
|
||||||
data = hopper().getdata()
|
|
||||||
|
|
||||||
len(data)
|
|
||||||
list(data)
|
|
||||||
|
|
||||||
assert data[0] == (20, 20, 70)
|
|
||||||
|
|
||||||
|
|
||||||
def test_mode() -> None:
|
|
||||||
def getdata(mode: str) -> tuple[float | tuple[int, ...], int, int]:
|
|
||||||
im = hopper(mode).resize((32, 30), Image.Resampling.NEAREST)
|
|
||||||
data = im.getdata()
|
|
||||||
return data[0], len(data), len(list(data))
|
|
||||||
|
|
||||||
assert getdata("1") == (0, 960, 960)
|
|
||||||
assert getdata("L") == (17, 960, 960)
|
|
||||||
assert getdata("I") == (17, 960, 960)
|
|
||||||
assert getdata("F") == (17.0, 960, 960)
|
|
||||||
assert getdata("RGB") == ((11, 13, 52), 960, 960)
|
|
||||||
assert getdata("RGBA") == ((11, 13, 52, 255), 960, 960)
|
|
||||||
assert getdata("CMYK") == ((244, 242, 203, 0), 960, 960)
|
|
||||||
assert getdata("YCbCr") == ((16, 147, 123), 960, 960)
|
|
|
@ -7,10 +7,19 @@ import pytest
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from .helper import assert_image_equal, hopper
|
from .helper import assert_image_equal, hopper, is_big_endian
|
||||||
|
|
||||||
|
|
||||||
def test_sanity() -> None:
|
def test_getdata_sanity() -> None:
|
||||||
|
data = hopper().getdata()
|
||||||
|
|
||||||
|
len(data)
|
||||||
|
list(data)
|
||||||
|
|
||||||
|
assert data[0] == (20, 20, 70)
|
||||||
|
|
||||||
|
|
||||||
|
def test_putdata_sanity() -> None:
|
||||||
im1 = hopper()
|
im1 = hopper()
|
||||||
|
|
||||||
data = list(im1.getdata())
|
data = list(im1.getdata())
|
||||||
|
@ -29,7 +38,23 @@ def test_sanity() -> None:
|
||||||
assert_image_equal(im1, im2)
|
assert_image_equal(im1, im2)
|
||||||
|
|
||||||
|
|
||||||
def test_long_integers() -> None:
|
def test_getdata_roundtrip() -> None:
|
||||||
|
def getdata(mode: str) -> tuple[float | tuple[int, ...], int, int]:
|
||||||
|
im = hopper(mode).resize((32, 30), Image.Resampling.NEAREST)
|
||||||
|
data = im.getdata()
|
||||||
|
return data[0], len(data), len(list(data))
|
||||||
|
|
||||||
|
assert getdata("1") == (0, 960, 960)
|
||||||
|
assert getdata("L") == (17, 960, 960)
|
||||||
|
assert getdata("I") == (17, 960, 960)
|
||||||
|
assert getdata("F") == (17.0, 960, 960)
|
||||||
|
assert getdata("RGB") == ((11, 13, 52), 960, 960)
|
||||||
|
assert getdata("RGBA") == ((11, 13, 52, 255), 960, 960)
|
||||||
|
assert getdata("CMYK") == ((244, 242, 203, 0), 960, 960)
|
||||||
|
assert getdata("YCbCr") == ((16, 147, 123), 960, 960)
|
||||||
|
|
||||||
|
|
||||||
|
def test_putdata_long_integers() -> None:
|
||||||
# see bug-200802-systemerror
|
# see bug-200802-systemerror
|
||||||
def put(value: int) -> float | tuple[int, ...] | None:
|
def put(value: int) -> float | tuple[int, ...] | None:
|
||||||
im = Image.new("RGBA", (1, 1))
|
im = Image.new("RGBA", (1, 1))
|
||||||
|
@ -46,19 +71,19 @@ def test_long_integers() -> None:
|
||||||
assert put(sys.maxsize) == (255, 255, 255, 127)
|
assert put(sys.maxsize) == (255, 255, 255, 127)
|
||||||
|
|
||||||
|
|
||||||
def test_pypy_performance() -> None:
|
def test_putdata_pypy_performance() -> None:
|
||||||
im = Image.new("L", (256, 256))
|
im = Image.new("L", (256, 256))
|
||||||
im.putdata(list(range(256)) * 256)
|
im.putdata(list(range(256)) * 256)
|
||||||
|
|
||||||
|
|
||||||
def test_mode_with_L_with_float() -> None:
|
def test_putdata_mode_with_L_with_float() -> None:
|
||||||
im = Image.new("L", (1, 1), 0)
|
im = Image.new("L", (1, 1), 0)
|
||||||
im.putdata([2.0])
|
im.putdata([2.0])
|
||||||
assert im.getpixel((0, 0)) == 2
|
assert im.getpixel((0, 0)) == 2
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("mode", ("I", "I;16", "I;16L", "I;16B"))
|
@pytest.mark.parametrize("mode", ("I", "I;16", "I;16L", "I;16B"))
|
||||||
def test_mode_i(mode: str) -> None:
|
def test_putdata_mode_I(mode: str) -> None:
|
||||||
src = hopper("L")
|
src = hopper("L")
|
||||||
data = list(src.getdata())
|
data = list(src.getdata())
|
||||||
im = Image.new(mode, src.size, 0)
|
im = Image.new(mode, src.size, 0)
|
||||||
|
@ -68,7 +93,7 @@ def test_mode_i(mode: str) -> None:
|
||||||
assert list(im.getdata()) == target
|
assert list(im.getdata()) == target
|
||||||
|
|
||||||
|
|
||||||
def test_mode_F() -> None:
|
def test_putdata_mode_F() -> None:
|
||||||
src = hopper("L")
|
src = hopper("L")
|
||||||
data = list(src.getdata())
|
data = list(src.getdata())
|
||||||
im = Image.new("F", src.size, 0)
|
im = Image.new("F", src.size, 0)
|
||||||
|
@ -79,7 +104,7 @@ def test_mode_F() -> None:
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("mode", ("BGR;15", "BGR;16", "BGR;24"))
|
@pytest.mark.parametrize("mode", ("BGR;15", "BGR;16", "BGR;24"))
|
||||||
def test_mode_BGR(mode: str) -> None:
|
def test_putdata_mode_BGR(mode: str) -> None:
|
||||||
data = [(16, 32, 49), (32, 32, 98)]
|
data = [(16, 32, 49), (32, 32, 98)]
|
||||||
with pytest.warns(DeprecationWarning):
|
with pytest.warns(DeprecationWarning):
|
||||||
im = Image.new(mode, (1, 2))
|
im = Image.new(mode, (1, 2))
|
||||||
|
@ -88,7 +113,7 @@ def test_mode_BGR(mode: str) -> None:
|
||||||
assert list(im.getdata()) == data
|
assert list(im.getdata()) == data
|
||||||
|
|
||||||
|
|
||||||
def test_array_B() -> None:
|
def test_putdata_array_B() -> None:
|
||||||
# shouldn't segfault
|
# shouldn't segfault
|
||||||
# see https://github.com/python-pillow/Pillow/issues/1008
|
# see https://github.com/python-pillow/Pillow/issues/1008
|
||||||
|
|
||||||
|
@ -99,7 +124,7 @@ def test_array_B() -> None:
|
||||||
assert len(im.getdata()) == len(arr)
|
assert len(im.getdata()) == len(arr)
|
||||||
|
|
||||||
|
|
||||||
def test_array_F() -> None:
|
def test_putdata_array_F() -> None:
|
||||||
# shouldn't segfault
|
# shouldn't segfault
|
||||||
# see https://github.com/python-pillow/Pillow/issues/1008
|
# see https://github.com/python-pillow/Pillow/issues/1008
|
||||||
|
|
||||||
|
@ -110,7 +135,7 @@ def test_array_F() -> None:
|
||||||
assert len(im.getdata()) == len(arr)
|
assert len(im.getdata()) == len(arr)
|
||||||
|
|
||||||
|
|
||||||
def test_not_flattened() -> None:
|
def test_putdata_not_flattened() -> None:
|
||||||
im = Image.new("L", (1, 1))
|
im = Image.new("L", (1, 1))
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
im.putdata([[0]])
|
im.putdata([[0]])
|
||||||
|
@ -123,3 +148,17 @@ def test_not_flattened() -> None:
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
im = Image.new("F", (1, 1))
|
im = Image.new("F", (1, 1))
|
||||||
im.putdata([[0]])
|
im.putdata([[0]])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("mode", Image.MODES + ["BGR;15", "BGR;16", "BGR;24"])
|
||||||
|
def test_getdata_putdata(mode: str) -> None:
|
||||||
|
if is_big_endian() and mode == "BGR;15":
|
||||||
|
pytest.xfail("Known failure of BGR;15 on big-endian")
|
||||||
|
im = hopper(mode)
|
||||||
|
if mode.startswith("BGR;"):
|
||||||
|
with pytest.warns(DeprecationWarning):
|
||||||
|
reloaded = Image.new(mode, im.size)
|
||||||
|
else:
|
||||||
|
reloaded = Image.new(mode, im.size)
|
||||||
|
reloaded.putdata(im.getdata())
|
||||||
|
assert_image_equal(im, reloaded)
|
Loading…
Reference in New Issue
Block a user