Skip to content

Commit 3baedf2

Browse files
authored
Deprecate getdata(), in favour of new get_flattened_data() (#9292)
1 parent b51a036 commit 3baedf2

22 files changed

+117
-66
lines changed

Tests/helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def convert_to_comparable(
5555
if a.mode == "P":
5656
new_a = Image.new("L", a.size)
5757
new_b = Image.new("L", b.size)
58-
new_a.putdata(a.getdata())
59-
new_b.putdata(b.getdata())
58+
new_a.putdata(a.get_flattened_data())
59+
new_b.putdata(b.get_flattened_data())
6060
elif a.mode == "I;16":
6161
new_a = a.convert("I")
6262
new_b = b.convert("I")

Tests/test_box_blur.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ def box_blur(image: Image.Image, radius: float = 1, n: int = 1) -> Image.Image:
2828

2929

3030
def assert_image(im: Image.Image, data: list[list[int]], delta: int = 0) -> None:
31-
it = iter(im.getdata())
31+
it = iter(im.get_flattened_data())
3232
for data_row in data:
33-
im_row = [next(it) for _ in range(im.size[0])]
33+
im_row = []
34+
for _ in range(im.width):
35+
im_v = next(it)
36+
assert isinstance(im_v, (int, float))
37+
im_row.append(im_v)
3438
if any(abs(data_v - im_v) > delta for data_v, im_v in zip(data_row, im_row)):
3539
assert im_row == data_row
3640
with pytest.raises(StopIteration):

Tests/test_file_avif.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ def test_read(self) -> None:
121121
assert image.size == (128, 128)
122122
assert image.format == "AVIF"
123123
assert image.get_format_mimetype() == "image/avif"
124-
image.getdata()
125124

126125
# generated with:
127126
# avifdec hopper.avif hopper_avif_write.png
@@ -143,7 +142,6 @@ def test_write_rgb(self, tmp_path: Path) -> None:
143142
assert reloaded.mode == "RGB"
144143
assert reloaded.size == (128, 128)
145144
assert reloaded.format == "AVIF"
146-
reloaded.getdata()
147145

148146
# avifdec hopper.avif avif/hopper_avif_write.png
149147
assert_image_similar_tofile(

Tests/test_file_libtiff.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def _assert_noerr(self, tmp_path: Path, im: ImageFile.ImageFile) -> None:
4242

4343
# Does the data actually load
4444
im.load()
45-
im.getdata()
4645

4746
assert isinstance(im, TiffImagePlugin.TiffImageFile)
4847
assert im._compression == "group4"

Tests/test_file_webp.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def test_read_rgb(self) -> None:
6060
assert image.size == (128, 128)
6161
assert image.format == "WEBP"
6262
image.load()
63-
image.getdata()
6463

6564
# generated with:
6665
# dwebp -ppm ../../Tests/images/hopper.webp -o hopper_webp_bits.ppm
@@ -77,7 +76,6 @@ def _roundtrip(
7776
assert image.size == (128, 128)
7877
assert image.format == "WEBP"
7978
image.load()
80-
image.getdata()
8179

8280
if mode == self.rgb_mode:
8381
# generated with: dwebp -ppm temp.webp -o hopper_webp_write.ppm

Tests/test_file_webp_alpha.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def test_read_rgba() -> None:
2929
assert image.size == (200, 150)
3030
assert image.format == "WEBP"
3131
image.load()
32-
image.getdata()
3332

3433
image.tobytes()
3534

@@ -60,7 +59,6 @@ def test_write_lossless_rgb(tmp_path: Path) -> None:
6059
assert image.size == pil_image.size
6160
assert image.format == "WEBP"
6261
image.load()
63-
image.getdata()
6462

6563
assert_image_equal(image, pil_image)
6664

@@ -83,7 +81,6 @@ def test_write_rgba(tmp_path: Path) -> None:
8381
assert image.size == (10, 10)
8482
assert image.format == "WEBP"
8583
image.load()
86-
image.getdata()
8784

8885
assert_image_similar(image, pil_image, 1.0)
8986

@@ -133,7 +130,6 @@ def test_write_unsupported_mode_PA(tmp_path: Path) -> None:
133130
assert image.format == "WEBP"
134131

135132
image.load()
136-
image.getdata()
137133
with Image.open(file_path) as im:
138134
target = im.convert("RGBA")
139135

Tests/test_file_webp_lossless.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,5 @@ def test_write_lossless_rgb(tmp_path: Path) -> None:
2424
assert image.size == (128, 128)
2525
assert image.format == "WEBP"
2626
image.load()
27-
image.getdata()
2827

2928
assert_image_equal(image, hopper(RGB_MODE))

Tests/test_format_lab.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ def test_white() -> None:
1313

1414
k = i.getpixel((0, 0))
1515

16-
L = i.getdata(0)
17-
a = i.getdata(1)
18-
b = i.getdata(2)
16+
L = i.get_flattened_data(0)
17+
a = i.get_flattened_data(1)
18+
b = i.get_flattened_data(2)
1919

2020
assert k == (255, 128, 128)
2121

22-
assert list(L) == [255] * 100
23-
assert list(a) == [128] * 100
24-
assert list(b) == [128] * 100
22+
assert L == (255,) * 100
23+
assert a == (128,) * 100
24+
assert b == (128,) * 100
2525

2626

2727
def test_green() -> None:

Tests/test_image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,10 +1181,10 @@ def test_roundtrip_bytes_method(self, mode: str) -> None:
11811181
assert reloaded.tobytes() == source_bytes
11821182

11831183
@pytest.mark.parametrize("mode", Image.MODES)
1184-
def test_getdata_putdata(self, mode: str) -> None:
1184+
def test_get_flattened_data_putdata(self, mode: str) -> None:
11851185
im = hopper(mode)
11861186
reloaded = Image.new(mode, im.size)
1187-
reloaded.putdata(im.getdata())
1187+
reloaded.putdata(im.get_flattened_data())
11881188
assert_image_equal(im, reloaded)
11891189

11901190

Tests/test_image_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test(mode: str) -> tuple[str, tuple[int, int], bool]:
7878
},
7979
)
8080
out = Image.fromarray(wrapped)
81-
return out.mode, out.size, list(i.getdata()) == list(out.getdata())
81+
return out.mode, out.size, i.get_flattened_data() == out.get_flattened_data()
8282

8383
# assert test("1") == ("1", (128, 100), True)
8484
assert test("L") == ("L", (128, 100), True)

0 commit comments

Comments
 (0)