From 87e458cdcb0487269480b105667efcee760813b9 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 15 Jun 2024 13:55:00 +1000 Subject: [PATCH] Removed PixelAccess protocol --- docs/reference/PixelAccess.rst | 42 +++++++++++++++++++++++++++++++--- src/PIL/Image.py | 31 +++---------------------- src/PIL/_imaging.pyi | 4 ++++ 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/docs/reference/PixelAccess.rst b/docs/reference/PixelAccess.rst index 026f488d8e1..c1130b5c257 100644 --- a/docs/reference/PixelAccess.rst +++ b/docs/reference/PixelAccess.rst @@ -44,7 +44,43 @@ Access using negative indexes is also possible. :: ----------------------------- .. class:: PixelAccess - :canonical: PIL.Image.PixelAccess + :canonical: PIL.core.PixelAccess - .. automethod:: PIL.Image.PixelAccess.__getitem__ - .. automethod:: PIL.Image.PixelAccess.__setitem__ + .. method:: __setitem__(self, xy, color): + + 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) + + .. method:: __getitem__(self, xy): + + Returns the pixel at x,y. The pixel is returned as a single + value for single band images or a tuple for multiple 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:: putpixel(self, xy, color): + + 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. In addition to this, RGB and RGBA tuples + are accepted for P and PA 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) + + .. method:: getpixel(self, xy): + + Returns the pixel at x,y. The pixel is returned as a single + value for single band images or a tuple for multiple 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. diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 5619915ea78..557a90e60bd 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -227,7 +227,7 @@ class Quantize(IntEnum): # 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 @@ def frombytes(self, data: bytes, decoder_name: str = "raw", *args) -> None: 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 @@ -910,7 +885,7 @@ def load(self) -> PixelAccess | None: operations. See :ref:`file-handling` for more information. :returns: An image access object. - :rtype: :py:class:`.PixelAccess` or :py:class:`.PyAccess` + :rtype: :ref:`PixelAccess` or :py:class:`.PyAccess` """ if self.im is not None and self.palette and self.palette.dirty: # realize palette diff --git a/src/PIL/_imaging.pyi b/src/PIL/_imaging.pyi index 1fe95441715..22ebf8f4615 100644 --- a/src/PIL/_imaging.pyi +++ b/src/PIL/_imaging.pyi @@ -10,6 +10,10 @@ class ImagingDraw: def __getattr__(self, name: str) -> Any: ... class PixelAccess: + def __getitem__(self, xy: tuple[int, int]) -> float | tuple[int, ...]: ... + def __setitem__( + self, xy: tuple[int, int], color: float | tuple[int, ...] + ) -> None: ... def __getattr__(self, name: str) -> Any: ... def font(image, glyphdata: bytes) -> ImagingFont: ...