Skip to content

Un-deprecate getdata(); have it return an ImageLinearAccess object - #9883

Open
akx wants to merge 1 commit into
python-pillow:mainfrom
akx:image-getdata-linear-access
Open

Un-deprecate getdata(); have it return an ImageLinearAccess object#9883
akx wants to merge 1 commit into
python-pillow:mainfrom
akx:image-getdata-linear-access

Conversation

@akx

@akx akx commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Follows up on #9292 to un-deprecate getdata(), since get_flattened_data() is not a perfect replacement for linear access to image pixels given its upfront memory and time costs. (See #9292 (comment) for rationales.)

Fixes #9261 (since getdata now returns a real, well-typed iterator/access object). Given x.py like

# pyright: strict
from __future__ import annotations
from PIL import Image
im = Image.new("RGB", (4, 4))
printed = list(im.getdata())

pyright fails on main:

(main) $ uvx --quiet --with pytest --with numpy pyright@1.1.411 x.py
  x.py:5:1 - error: Type of "printed" is partially unknown
    Type of "printed" is "list[Unknown]" (reportUnknownVariableType)
  x.py:5:16 - error: Argument of type "ImagingCore" cannot be assigned to parameter "iterable" of type "Iterable[_T@list]" in function "__init__"
    "ImagingCore" is incompatible with protocol "Iterable[_T@list]"
      "__iter__" is not present (reportArgumentType)
2 errors, 0 warnings, 0 informations

but is fine on this branch:

(image-getdata-linear-access) $ uvx --quiet --with pytest --with numpy pyright@1.1.411 x.py
0 errors, 0 warnings, 0 informations

The optimizations from #9881 still help this too and don't conflict here.

@akx

akx commented Aug 21, 2026

Copy link
Copy Markdown
Contributor Author

Didn't think I'd bump into a PyPy bug implementing this, but here we are... pypy/pypy#5526

@akx
akx force-pushed the image-getdata-linear-access branch from d8ff245 to ecf4175 Compare August 21, 2026 11:44
@akx
akx force-pushed the image-getdata-linear-access branch from ecf4175 to 3756d89 Compare August 21, 2026 12:29
@akx
akx marked this pull request as ready for review August 21, 2026 12:41
@radarhere

Copy link
Copy Markdown
Member

Could you identify exactly what workaround you added for that, so that one day when the PyPy fix has propagated, we are able to remove it?


def test_getdata_does_not_expose_the_image_core() -> None:
hopper_data = hopper().getdata()
# "Weird" core bits are not exposed:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by 'weird'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants