Skip to content

Commit 9fb0792

Browse files
committed
Remove use of deprecated Pillow function
Fixes #170 by removing use of `PIL.Image.getdata()`. Rather than using the suggested replacement of `get_flattened_data` which would, require checking the version of Pillow, this uses `PIL.Image.tobytes()`, which is available on all versions of Pillow. `.tobytes` is also significantly faster, as it doesn't require Python list iteration. Since the image is explicitly converted to "RGBA" format, the bytes representation is just a linear list of the pixel values anyway.
1 parent ad7522c commit 9fb0792

2 files changed

Lines changed: 2 additions & 7 deletions

File tree

pixelmatch/contrib/PIL.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,7 @@ def from_PIL_to_raw_data(pil_img: Image) -> MutableImageSequence:
7979
:param pil_img:
8080
:return:
8181
"""
82-
# getdata() returns ImagingCore which is iterable at runtime
83-
return [
84-
item
85-
for sublist in pil_img.convert("RGBA").getdata() # type: ignore[attr-defined]
86-
for item in sublist
87-
]
82+
return list(pil_img.convert("RGBA").tobytes())
8883

8984

9085
def to_PIL_from_raw_data(

test_pixelmatch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def pil_to_flatten_data(img):
1919
"""
2020
Convert data from [(R1, G1, B1, A1), (R2, G2, B2, A2)] to [R1, G1, B1, A1, R2, G2, B2, A2]
2121
"""
22-
return [x for p in img.convert("RGBA").getdata() for x in p]
22+
return list(img.convert("RGBA").tobytes())
2323

2424

2525
testdata = [

0 commit comments

Comments
 (0)