Skip to content

Commit

Permalink
Removed PixelAccess protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jun 15, 2024
1 parent 20ce7ad commit 5ea5da3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 31 deletions.
42 changes: 39 additions & 3 deletions docs/reference/PixelAccess.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
31 changes: 3 additions & 28 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class Quantize(IntEnum):
# Registries

if TYPE_CHECKING:
from . import ImageFile
from . import ImageFile, PyAccess
ID: list[str] = []
OPEN: dict[
str,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/PIL/_imaging.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down

0 comments on commit 5ea5da3

Please sign in to comment.