diff --git a/docs/reference/PixelAccess.rst b/docs/reference/PixelAccess.rst index 026f488d8..1ac3d034b 100644 --- a/docs/reference/PixelAccess.rst +++ b/docs/reference/PixelAccess.rst @@ -44,7 +44,23 @@ Access using negative indexes is also possible. :: ----------------------------- .. class:: PixelAccess - :canonical: PIL.Image.PixelAccess + :canonical: PIL.Image.core.PixelAccess - .. automethod:: PIL.Image.PixelAccess.__getitem__ - .. automethod:: PIL.Image.PixelAccess.__setitem__ + .. method:: __getitem__(self, xy: tuple[int, int]) -> float | tuple[int, ...] + + Returns the pixel at x,y. The pixel is returned as a single + value for single band images or a tuple for multi-band images. + + :param xy: The pixel coordinate, given as (x, y). + :returns: a pixel value for single band images, a tuple of + pixel values for multiband images. + + .. method:: __setitem__(self, xy: tuple[int, int], color: float | tuple[int, ...]) -> None + + Modifies the pixel at x,y. The color is given as a single + numerical value for single band images, and a tuple for + multi-band images. + + :param xy: The pixel coordinate, given as (x, y). + :param color: The pixel value according to its mode, + e.g. tuple (r, g, b) for RGB mode. diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 5619915ea..26849707e 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -227,7 +227,7 @@ if hasattr(core, "DEFAULT_STRATEGY"): # Registries if TYPE_CHECKING: - from . import ImageFile + from . import ImageFile, PyAccess ID: list[str] = [] OPEN: dict[ str, @@ -512,31 +512,6 @@ def _getscaleoffset(expr): # Implementation wrapper -class PixelAccess(Protocol): - def __getitem__(self, xy: tuple[int, int]) -> float | tuple[int, ...]: - """ - Returns the pixel at x,y. The pixel is returned as a single - value for single band images or a tuple for multi-band images. - - :param xy: The pixel coordinate, given as (x, y). - :returns: a pixel value for single band images, a tuple of - pixel values for multiband images. - """ - raise NotImplementedError() - - def __setitem__(self, xy: tuple[int, int], color: float | tuple[int, ...]) -> None: - """ - Modifies the pixel at x,y. The color is given as a single - numerical value for single band images, and a tuple for - multi-band images. - - :param xy: The pixel coordinate, given as (x, y). - :param color: The pixel value according to its mode, - e.g. tuple (r, g, b) for RGB mode. - """ - raise NotImplementedError() - - class SupportsGetData(Protocol): def getdata( self, @@ -897,7 +872,7 @@ class Image: msg = "cannot decode image data" raise ValueError(msg) - def load(self) -> PixelAccess | None: + def load(self) -> core.PixelAccess | PyAccess.PyAccess | None: """ Allocates storage for the image and loads the pixel data. In normal cases, you don't need to call this method, since the diff --git a/src/PIL/_imaging.pyi b/src/PIL/_imaging.pyi index 1fe954417..1a0184069 100644 --- a/src/PIL/_imaging.pyi +++ b/src/PIL/_imaging.pyi @@ -10,7 +10,10 @@ class ImagingDraw: def __getattr__(self, name: str) -> Any: ... class PixelAccess: - def __getattr__(self, name: str) -> Any: ... + def __getitem__(self, xy: tuple[int, int]) -> float | tuple[int, ...]: ... + def __setitem__( + self, xy: tuple[int, int], color: float | tuple[int, ...] + ) -> None: ... def font(image, glyphdata: bytes) -> ImagingFont: ... def __getattr__(name: str) -> Any: ...