From 5f805c39cccd6ccc5c695a885e274f8a2b4624f9 Mon Sep 17 00:00:00 2001 From: Nulano Date: Mon, 29 Apr 2024 23:19:36 +0200 Subject: [PATCH 01/13] Added type hints for PixelAccess methods and others --- docs/reference/ImageDraw.rst | 18 +----------- docs/reference/PixelAccess.rst | 41 ++------------------------ docs/reference/PyAccess.rst | 1 + src/PIL/Image.py | 53 +++++++++++++++++++++++++--------- src/PIL/ImageDraw.py | 13 +++++++-- src/PIL/ImageGrab.py | 15 +++++++--- src/PIL/PyAccess.py | 48 ++++++++++++++++++------------ 7 files changed, 97 insertions(+), 92 deletions(-) diff --git a/docs/reference/ImageDraw.rst b/docs/reference/ImageDraw.rst index 4ccfacae7..e7339ecbe 100644 --- a/docs/reference/ImageDraw.rst +++ b/docs/reference/ImageDraw.rst @@ -679,23 +679,7 @@ Methods :param hints: An optional list of hints. :returns: A (drawing context, drawing resource factory) tuple. -.. py:method:: floodfill(image, xy, value, border=None, thresh=0) - - .. warning:: This method is experimental. - - Fills a bounded region with a given color. - - :param image: Target image. - :param xy: Seed position (a 2-item coordinate tuple). - :param value: Fill color. - :param border: Optional border value. If given, the region consists of - pixels with a color different from the border color. If not given, - the region consists of pixels having the same color as the seed - pixel. - :param thresh: Optional threshold value which specifies a maximum - tolerable difference of a pixel value from the 'background' in - order for it to be replaced. Useful for filling regions of non- - homogeneous, but similar, colors. +.. autofunction:: PIL.ImageDraw.floodfill .. _BCP 47 language code: https://www.w3.org/International/articles/language-tags/ .. _OpenType docs: https://learn.microsoft.com/en-us/typography/opentype/spec/featurelist diff --git a/docs/reference/PixelAccess.rst b/docs/reference/PixelAccess.rst index 04d6f5dcd..026f488d8 100644 --- a/docs/reference/PixelAccess.rst +++ b/docs/reference/PixelAccess.rst @@ -44,42 +44,7 @@ Access using negative indexes is also possible. :: ----------------------------- .. class:: PixelAccess + :canonical: PIL.Image.PixelAccess - .. 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. + .. automethod:: PIL.Image.PixelAccess.__getitem__ + .. automethod:: PIL.Image.PixelAccess.__setitem__ diff --git a/docs/reference/PyAccess.rst b/docs/reference/PyAccess.rst index ed58ca3a5..04b2a47ee 100644 --- a/docs/reference/PyAccess.rst +++ b/docs/reference/PyAccess.rst @@ -44,3 +44,4 @@ Access using negative indexes is also possible. :: .. autoclass:: PIL.PyAccess.PyAccess() :members: + :special-members: __getitem__, __setitem__ diff --git a/src/PIL/Image.py b/src/PIL/Image.py index a17edfa39..c1b8a2b2f 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -41,7 +41,7 @@ import warnings from collections.abc import Callable, MutableMapping from enum import IntEnum from types import ModuleType -from typing import IO, TYPE_CHECKING, Any, Literal, Protocol, cast +from typing import IO, TYPE_CHECKING, Any, Literal, Protocol, SupportsInt, cast # VERSION was removed in Pillow 6.0.0. # PILLOW_VERSION was removed in Pillow 9.0.0. @@ -482,6 +482,31 @@ 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 Image: """ This class represents an image object. To create @@ -834,7 +859,7 @@ class Image: msg = "cannot decode image data" raise ValueError(msg) - def load(self): + def load(self) -> PixelAccess | None: """ Allocates storage for the image and loads the pixel data. In normal cases, you don't need to call this method, since the @@ -847,7 +872,7 @@ class Image: operations. See :ref:`file-handling` for more information. :returns: An image access object. - :rtype: :ref:`PixelAccess` or :py:class:`PIL.PyAccess` + :rtype: :py:class:`.PixelAccess` or :py:class:`.PyAccess` """ if self.im is not None and self.palette and self.palette.dirty: # realize palette @@ -876,6 +901,7 @@ class Image: if self.pyaccess: return self.pyaccess return self.im.pixel_access(self.readonly) + return None def verify(self): """ @@ -1485,7 +1511,7 @@ class Image: self._exif._loaded = False self.getexif() - def get_child_images(self): + def get_child_images(self) -> list[ImageFile.ImageFile]: child_images = [] exif = self.getexif() ifds = [] @@ -1509,10 +1535,7 @@ class Image: fp = self.fp thumbnail_offset = ifd.get(513) if thumbnail_offset is not None: - try: - thumbnail_offset += self._exif_offset - except AttributeError: - pass + thumbnail_offset += getattr(self, "_exif_offset", 0) self.fp.seek(thumbnail_offset) data = self.fp.read(ifd.get(514)) fp = io.BytesIO(data) @@ -1578,7 +1601,7 @@ class Image: or "transparency" in self.info ) - def apply_transparency(self): + def apply_transparency(self) -> None: """ If a P mode image has a "transparency" key in the info dictionary, remove the key and instead apply the transparency to the palette. @@ -1590,6 +1613,7 @@ class Image: from . import ImagePalette palette = self.getpalette("RGBA") + assert palette is not None transparency = self.info["transparency"] if isinstance(transparency, bytes): for i, alpha in enumerate(transparency): @@ -1601,7 +1625,9 @@ class Image: del self.info["transparency"] - def getpixel(self, xy): + def getpixel( + self, xy: tuple[SupportsInt, SupportsInt] + ) -> float | tuple[int, ...] | None: """ Returns the pixel value at a given position. @@ -1865,7 +1891,7 @@ class Image: lut = [round(i) for i in lut] return self._new(self.im.point(lut, mode)) - def putalpha(self, alpha): + def putalpha(self, alpha: Image | int) -> None: """ Adds or replaces the alpha layer in this image. If the image does not have an alpha layer, it's converted to "LA" or "RGBA". @@ -1912,6 +1938,7 @@ class Image: alpha = alpha.convert("L") else: # constant alpha + alpha = cast(int, alpha) # see python/typing#1013 try: self.im.fillband(band, alpha) except (AttributeError, ValueError): @@ -1975,7 +2002,7 @@ class Image: self.palette.mode = "RGB" self.load() # install new palette - def putpixel(self, xy, value): + def putpixel(self, xy: tuple[int, int], value: float | tuple[int, ...]) -> None: """ Modifies the pixel at the given position. The color is given as a single numerical value for single-band images, and a tuple for @@ -2015,7 +2042,7 @@ class Image: value = value[:3] value = self.palette.getcolor(value, self) if self.mode == "PA": - value = (value, alpha) + value = (value, alpha) # type: ignore[assignment] return self.im.putpixel(xy, value) def remap_palette(self, dest_map, source_palette=None): diff --git a/src/PIL/ImageDraw.py b/src/PIL/ImageDraw.py index d3efe6486..5c179000c 100644 --- a/src/PIL/ImageDraw.py +++ b/src/PIL/ImageDraw.py @@ -898,9 +898,17 @@ def getdraw(im=None, hints=None): return im, handler -def floodfill(image: Image.Image, xy, value, border=None, thresh=0) -> None: +def floodfill( + image: Image.Image, + xy: tuple[int, int], + value: float | tuple[int, ...], + border: float | tuple[int, ...] | None = None, + thresh: float = 0, +) -> None: """ - (experimental) Fills a bounded region with a given color. + .. warning:: This method is experimental. + + Fills a bounded region with a given color. :param image: Target image. :param xy: Seed position (a 2-item coordinate tuple). See @@ -918,6 +926,7 @@ def floodfill(image: Image.Image, xy, value, border=None, thresh=0) -> None: # based on an implementation by Eric S. Raymond # amended by yo1995 @20180806 pixel = image.load() + assert pixel is not None x, y = xy try: background = pixel[x, y] diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 3f3be706d..f52dd301d 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -22,11 +22,17 @@ import shutil import subprocess import sys import tempfile +from typing import Union, cast from . import Image -def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=None): +def grab( + bbox: tuple[int, int, int, int] | None = None, + include_layered_windows: bool = False, + all_screens: bool = False, + xdisplay: str | None = None, +) -> Image.Image: if xdisplay is None: if sys.platform == "darwin": fh, filepath = tempfile.mkstemp(".png") @@ -36,7 +42,7 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N left, top, right, bottom = bbox args += ["-R", f"{left},{top},{right-left},{bottom-top}"] subprocess.call(args + ["-x", filepath]) - im = Image.open(filepath) + im: Image.Image = Image.open(filepath) im.load() os.unlink(filepath) if bbox: @@ -63,6 +69,7 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N left, top, right, bottom = bbox im = im.crop((left - x0, top - y0, right - x0, bottom - y0)) return im + xdisplay = cast(Union[str, None], xdisplay) # type: ignore[redundant-cast, unused-ignore] try: if not Image.core.HAVE_XCB: msg = "Pillow was built without XCB support" @@ -77,7 +84,7 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N fh, filepath = tempfile.mkstemp(".png") os.close(fh) subprocess.call(["gnome-screenshot", "-f", filepath]) - im = Image.open(filepath) + im: Image.Image = Image.open(filepath) im.load() os.unlink(filepath) if bbox: @@ -94,7 +101,7 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N return im -def grabclipboard(): +def grabclipboard() -> Image.Image | list[str] | None: if sys.platform == "darwin": fh, filepath = tempfile.mkstemp(".png") os.close(fh) diff --git a/src/PIL/PyAccess.py b/src/PIL/PyAccess.py index 2c831913d..af9595666 100644 --- a/src/PIL/PyAccess.py +++ b/src/PIL/PyAccess.py @@ -22,6 +22,7 @@ from __future__ import annotations import logging import sys +from typing import TYPE_CHECKING from ._deprecate import deprecate @@ -50,7 +51,7 @@ logger = logging.getLogger(__name__) class PyAccess: - def __init__(self, img, readonly=False): + def __init__(self, img: Image.Image, readonly: bool = False) -> None: deprecate("PyAccess", 11) vals = dict(img.im.unsafe_ptrs) self.readonly = readonly @@ -70,14 +71,15 @@ class PyAccess: # logger.debug("%s", vals) self._post_init() - def _post_init(self): + def _post_init(self) -> None: pass - def __setitem__(self, xy, color): + 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 + 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). See :ref:`coordinate-system`. @@ -104,11 +106,11 @@ class PyAccess: color = color[:3] color = self._palette.getcolor(color, self._img) if self._im.mode == "PA": - color = (color, alpha) + color = (color, alpha) # type: ignore[assignment] return self.set_pixel(x, y, color) - def __getitem__(self, xy): + 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 multiple band @@ -130,13 +132,19 @@ class PyAccess: putpixel = __setitem__ getpixel = __getitem__ - def check_xy(self, xy): + def check_xy(self, xy: tuple[int, int]) -> tuple[int, int]: (x, y) = xy if not (0 <= x < self.xsize and 0 <= y < self.ysize): msg = "pixel location out of range" raise ValueError(msg) return xy + def get_pixel(self, x: int, y: int) -> float | tuple[int, ...]: + raise NotImplementedError() + + def set_pixel(self, x: int, y: int, color: float | tuple[int, ...]) -> None: + raise NotImplementedError() + class _PyAccess32_2(PyAccess): """PA, LA, stored in first and last bytes of a 32 bit word""" @@ -144,7 +152,7 @@ class _PyAccess32_2(PyAccess): def _post_init(self, *args, **kwargs): self.pixels = ffi.cast("struct Pixel_RGBA **", self.image32) - def get_pixel(self, x, y): + def get_pixel(self, x: int, y: int) -> tuple[int, int]: pixel = self.pixels[y][x] return pixel.r, pixel.a @@ -161,7 +169,7 @@ class _PyAccess32_3(PyAccess): def _post_init(self, *args, **kwargs): self.pixels = ffi.cast("struct Pixel_RGBA **", self.image32) - def get_pixel(self, x, y): + def get_pixel(self, x: int, y: int) -> tuple[int, int, int]: pixel = self.pixels[y][x] return pixel.r, pixel.g, pixel.b @@ -180,7 +188,7 @@ class _PyAccess32_4(PyAccess): def _post_init(self, *args, **kwargs): self.pixels = ffi.cast("struct Pixel_RGBA **", self.image32) - def get_pixel(self, x, y): + def get_pixel(self, x: int, y: int) -> tuple[int, int, int, int]: pixel = self.pixels[y][x] return pixel.r, pixel.g, pixel.b, pixel.a @@ -199,7 +207,7 @@ class _PyAccess8(PyAccess): def _post_init(self, *args, **kwargs): self.pixels = self.image8 - def get_pixel(self, x, y): + def get_pixel(self, x: int, y: int) -> int: return self.pixels[y][x] def set_pixel(self, x, y, color): @@ -217,7 +225,7 @@ class _PyAccessI16_N(PyAccess): def _post_init(self, *args, **kwargs): self.pixels = ffi.cast("unsigned short **", self.image) - def get_pixel(self, x, y): + def get_pixel(self, x: int, y: int) -> int: return self.pixels[y][x] def set_pixel(self, x, y, color): @@ -235,7 +243,7 @@ class _PyAccessI16_L(PyAccess): def _post_init(self, *args, **kwargs): self.pixels = ffi.cast("struct Pixel_I16 **", self.image) - def get_pixel(self, x, y): + def get_pixel(self, x: int, y: int) -> int: pixel = self.pixels[y][x] return pixel.l + pixel.r * 256 @@ -256,7 +264,7 @@ class _PyAccessI16_B(PyAccess): def _post_init(self, *args, **kwargs): self.pixels = ffi.cast("struct Pixel_I16 **", self.image) - def get_pixel(self, x, y): + def get_pixel(self, x: int, y: int) -> int: pixel = self.pixels[y][x] return pixel.l * 256 + pixel.r @@ -277,7 +285,7 @@ class _PyAccessI32_N(PyAccess): def _post_init(self, *args, **kwargs): self.pixels = self.image32 - def get_pixel(self, x, y): + def get_pixel(self, x: int, y: int) -> int: return self.pixels[y][x] def set_pixel(self, x, y, color): @@ -296,7 +304,7 @@ class _PyAccessI32_Swap(PyAccess): chars[0], chars[1], chars[2], chars[3] = chars[3], chars[2], chars[1], chars[0] return ffi.cast("int *", chars)[0] - def get_pixel(self, x, y): + def get_pixel(self, x: int, y: int) -> int: return self.reverse(self.pixels[y][x]) def set_pixel(self, x, y, color): @@ -309,7 +317,7 @@ class _PyAccessF(PyAccess): def _post_init(self, *args, **kwargs): self.pixels = ffi.cast("float **", self.image32) - def get_pixel(self, x, y): + def get_pixel(self, x: int, y: int) -> float: return self.pixels[y][x] def set_pixel(self, x, y, color): @@ -357,9 +365,13 @@ else: mode_map["I;32B"] = _PyAccessI32_N -def new(img, readonly=False): +def new(img: Image.Image, readonly: bool = False) -> PyAccess | None: access_type = mode_map.get(img.mode, None) if not access_type: logger.debug("PyAccess Not Implemented: %s", img.mode) return None return access_type(img, readonly) + + +if TYPE_CHECKING: + from . import Image From 74b87ae7486e475c032044a648c33605514844ed Mon Sep 17 00:00:00 2001 From: Nulano Date: Tue, 30 Apr 2024 16:32:29 +0200 Subject: [PATCH 02/13] Move import to top of file --- src/PIL/PyAccess.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/PIL/PyAccess.py b/src/PIL/PyAccess.py index af9595666..cc8e2e359 100644 --- a/src/PIL/PyAccess.py +++ b/src/PIL/PyAccess.py @@ -49,6 +49,9 @@ except ImportError as ex: logger = logging.getLogger(__name__) +if TYPE_CHECKING: + from . import Image + class PyAccess: def __init__(self, img: Image.Image, readonly: bool = False) -> None: @@ -371,7 +374,3 @@ def new(img: Image.Image, readonly: bool = False) -> PyAccess | None: logger.debug("PyAccess Not Implemented: %s", img.mode) return None return access_type(img, readonly) - - -if TYPE_CHECKING: - from . import Image From c2cb9445141476283ff0a94be06ad70d4422e535 Mon Sep 17 00:00:00 2001 From: Nulano Date: Tue, 30 Apr 2024 16:32:44 +0200 Subject: [PATCH 03/13] Ignore incorrect mypy warning --- src/PIL/ImageGrab.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index f52dd301d..f16e95544 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -84,7 +84,7 @@ def grab( fh, filepath = tempfile.mkstemp(".png") os.close(fh) subprocess.call(["gnome-screenshot", "-f", filepath]) - im: Image.Image = Image.open(filepath) + im: Image.Image = Image.open(filepath) # type: ignore[no-redef, unused-ignore] im.load() os.unlink(filepath) if bbox: From 7e14364cee9e5f78d0bb658dbf470a4e7e580e34 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 1 May 2024 22:03:33 +1000 Subject: [PATCH 04/13] putalpha does not allow other color values --- src/PIL/Image.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index da61ef4dd..5416524c0 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1933,8 +1933,7 @@ class Image: The new layer must be either "L" or "1". :param alpha: The new alpha layer. This can either be an "L" or "1" - image having the same size as this image, or an integer or - other color value. + image having the same size as this image, or an integer. """ self._ensure_mutable() From eb56f3ed56c9f72e5579943f9c87a2fcc9846587 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 30 May 2024 23:20:13 +1000 Subject: [PATCH 05/13] Removed ignores --- src/PIL/Image.py | 5 ++--- src/PIL/ImageGrab.py | 5 +++-- src/PIL/PyAccess.py | 5 ++--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 5416524c0..faf70b718 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2074,9 +2074,8 @@ class Image: if self.mode == "PA": alpha = value[3] if len(value) == 4 else 255 value = value[:3] - value = self.palette.getcolor(value, self) - if self.mode == "PA": - value = (value, alpha) # type: ignore[assignment] + palette_index = self.palette.getcolor(value, self) + value = (palette_index, alpha) if self.mode == "PA" else palette_index return self.im.putpixel(xy, value) def remap_palette(self, dest_map, source_palette=None): diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index f16e95544..96a28bb35 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -33,6 +33,7 @@ def grab( all_screens: bool = False, xdisplay: str | None = None, ) -> Image.Image: + im: Image.Image if xdisplay is None: if sys.platform == "darwin": fh, filepath = tempfile.mkstemp(".png") @@ -42,7 +43,7 @@ def grab( left, top, right, bottom = bbox args += ["-R", f"{left},{top},{right-left},{bottom-top}"] subprocess.call(args + ["-x", filepath]) - im: Image.Image = Image.open(filepath) + im = Image.open(filepath) im.load() os.unlink(filepath) if bbox: @@ -84,7 +85,7 @@ def grab( fh, filepath = tempfile.mkstemp(".png") os.close(fh) subprocess.call(["gnome-screenshot", "-f", filepath]) - im: Image.Image = Image.open(filepath) # type: ignore[no-redef, unused-ignore] + im = Image.open(filepath) im.load() os.unlink(filepath) if bbox: diff --git a/src/PIL/PyAccess.py b/src/PIL/PyAccess.py index cc8e2e359..5663a7e48 100644 --- a/src/PIL/PyAccess.py +++ b/src/PIL/PyAccess.py @@ -107,9 +107,8 @@ class PyAccess: if self._im.mode == "PA": alpha = color[3] if len(color) == 4 else 255 color = color[:3] - color = self._palette.getcolor(color, self._img) - if self._im.mode == "PA": - color = (color, alpha) # type: ignore[assignment] + palette_index = self._palette.getcolor(color, self._img) + color = (palette_index, alpha) if self._im.mode == "PA" else palette_index return self.set_pixel(x, y, color) From 2a2033eea1bd36637645f211239033bb7fad482d Mon Sep 17 00:00:00 2001 From: Nulano Date: Wed, 12 Jun 2024 21:35:22 +0200 Subject: [PATCH 06/13] mypy fixes after merge --- src/PIL/Image.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 76ea48510..6dee3fe23 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1128,7 +1128,10 @@ class Image: del new_im.info["transparency"] if trns is not None: try: - new_im.info["transparency"] = new_im.palette.getcolor(trns, new_im) + new_im.info["transparency"] = new_im.palette.getcolor( + cast(Tuple[int, int, int], trns), # trns was converted to RGB + new_im, + ) except Exception: # if we can't make a transparent color, don't leave the old # transparency hanging around to mess us up. @@ -1178,7 +1181,10 @@ class Image: if trns is not None: if new_im.mode == "P": try: - new_im.info["transparency"] = new_im.palette.getcolor(trns, new_im) + new_im.info["transparency"] = new_im.palette.getcolor( + cast(Tuple[int, int, int], trns), # trns was converted to RGB + new_im, + ) except ValueError as e: del new_im.info["transparency"] if str(e) != "cannot allocate more than 256 colors": From 238110303c7bf829c0e56e5f721bcd80ad715699 Mon Sep 17 00:00:00 2001 From: Nulano Date: Wed, 12 Jun 2024 22:17:10 +0200 Subject: [PATCH 07/13] getpixel and putpixel also support a list argument --- src/PIL/Image.py | 6 ++++-- src/PIL/PyAccess.py | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 6dee3fe23..9c60bbb7a 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1676,7 +1676,7 @@ class Image: del self.info["transparency"] def getpixel( - self, xy: tuple[SupportsInt, SupportsInt] + self, xy: tuple[int, int] | list[int] ) -> float | tuple[int, ...] | None: """ Returns the pixel value at a given position. @@ -2058,7 +2058,9 @@ class Image: self.palette.mode = "RGB" self.load() # install new palette - def putpixel(self, xy: tuple[int, int], value: float | tuple[int, ...]) -> None: + def putpixel( + self, xy: tuple[int, int], value: float | tuple[int, ...] | list[int] + ) -> None: """ Modifies the pixel at the given position. The color is given as a single numerical value for single-band images, and a tuple for diff --git a/src/PIL/PyAccess.py b/src/PIL/PyAccess.py index 5663a7e48..d41f00aea 100644 --- a/src/PIL/PyAccess.py +++ b/src/PIL/PyAccess.py @@ -77,7 +77,11 @@ class PyAccess: def _post_init(self) -> None: pass - def __setitem__(self, xy: tuple[int, int], color: float | tuple[int, ...]) -> None: + def __setitem__( + self, + xy: tuple[int, int] | list[int], + color: float | tuple[int, ...] | list[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 @@ -112,7 +116,7 @@ class PyAccess: return self.set_pixel(x, y, color) - def __getitem__(self, xy: tuple[int, int]) -> float | tuple[int, ...]: + def __getitem__(self, xy: tuple[int, int] | list[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 multiple band From 0a2baab6c1744a42422713858bc2bc2872805794 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Jun 2024 20:19:17 +0000 Subject: [PATCH 08/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/PIL/Image.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 9c60bbb7a..5619915ea 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -48,7 +48,6 @@ from typing import ( Literal, Protocol, Sequence, - SupportsInt, Tuple, cast, ) From 20ce7ad9f8c23bf6b02fd0a68cd39b20f12b58b7 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 13 Jun 2024 19:36:32 +1000 Subject: [PATCH 09/13] Updated type hint --- src/PIL/PyAccess.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/PIL/PyAccess.py b/src/PIL/PyAccess.py index d41f00aea..3be1ccace 100644 --- a/src/PIL/PyAccess.py +++ b/src/PIL/PyAccess.py @@ -148,7 +148,9 @@ class PyAccess: def get_pixel(self, x: int, y: int) -> float | tuple[int, ...]: raise NotImplementedError() - def set_pixel(self, x: int, y: int, color: float | tuple[int, ...]) -> None: + def set_pixel( + self, x: int, y: int, color: float | tuple[int, ...] | list[int] + ) -> None: raise NotImplementedError() From b64847e07fd32f8865f45600f663655116d46840 Mon Sep 17 00:00:00 2001 From: Nulano Date: Wed, 19 Jun 2024 21:48:48 +0200 Subject: [PATCH 10/13] Do not use a protocol for PixelAccess object --- docs/reference/PixelAccess.rst | 22 +++++++++++++++++++--- src/PIL/Image.py | 29 ++--------------------------- src/PIL/_imaging.pyi | 5 ++++- 3 files changed, 25 insertions(+), 31 deletions(-) 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: ... From ded404507bf8e8ae33ee194e9e1dc275e25edc82 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 22 Jun 2024 17:02:19 +1000 Subject: [PATCH 11/13] Removed ignores --- src/PIL/ImageGrab.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 96a28bb35..aa53abd20 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -22,7 +22,6 @@ import shutil import subprocess import sys import tempfile -from typing import Union, cast from . import Image @@ -70,15 +69,15 @@ def grab( left, top, right, bottom = bbox im = im.crop((left - x0, top - y0, right - x0, bottom - y0)) return im - xdisplay = cast(Union[str, None], xdisplay) # type: ignore[redundant-cast, unused-ignore] + display_name: str | None = xdisplay try: if not Image.core.HAVE_XCB: msg = "Pillow was built without XCB support" raise OSError(msg) - size, data = Image.core.grabscreen_x11(xdisplay) + size, data = Image.core.grabscreen_x11(display_name) except OSError: if ( - xdisplay is None + display_name is None and sys.platform not in ("darwin", "win32") and shutil.which("gnome-screenshot") ): From d0d53d4bac853acaad51af7d6aa9582cad59945a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 22 Jun 2024 17:02:28 +1000 Subject: [PATCH 12/13] Added type hints to tests --- Tests/test_imagegrab.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index e23adeb70..5dfa51697 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -89,6 +89,7 @@ $bmp = New-Object Drawing.Bitmap 200, 200 p.communicate() im = ImageGrab.grabclipboard() + assert isinstance(im, list) assert len(im) == 1 assert os.path.samefile(im[0], "Tests/images/hopper.gif") @@ -105,6 +106,7 @@ $ms = new-object System.IO.MemoryStream(, $bytes) p.communicate() im = ImageGrab.grabclipboard() + assert isinstance(im, Image.Image) assert_image_equal_tofile(im, "Tests/images/hopper.png") @pytest.mark.skipif( @@ -120,6 +122,7 @@ $ms = new-object System.IO.MemoryStream(, $bytes) with open(image_path, "rb") as fp: subprocess.call(["wl-copy"], stdin=fp) im = ImageGrab.grabclipboard() + assert isinstance(im, Image.Image) assert_image_equal_tofile(im, image_path) @pytest.mark.skipif( From ab183958188e38b6501410b4f34a561fbea0ba56 Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Tue, 25 Jun 2024 07:13:17 +1000 Subject: [PATCH 13/13] Added comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ondrej Baranovič --- src/PIL/ImageGrab.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index aa53abd20..e27ca7e50 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -69,6 +69,7 @@ def grab( left, top, right, bottom = bbox im = im.crop((left - x0, top - y0, right - x0, bottom - y0)) return im + # Cast to Optional[str] needed for Windows and macOS. display_name: str | None = xdisplay try: if not Image.core.HAVE_XCB: