Skip to content

Commit 04bca65

Browse files
committed
Added type hints
1 parent 95a69ec commit 04bca65

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

src/PIL/Image.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,7 @@ def _reload_exif(self) -> None:
15111511
self._exif._loaded = False
15121512
self.getexif()
15131513

1514-
def get_child_images(self):
1514+
def get_child_images(self) -> list[ImageFile.ImageFile]:
15151515
child_images = []
15161516
exif = self.getexif()
15171517
ifds = []
@@ -1535,10 +1535,7 @@ def get_child_images(self):
15351535
fp = self.fp
15361536
thumbnail_offset = ifd.get(513)
15371537
if thumbnail_offset is not None:
1538-
try:
1539-
thumbnail_offset += self._exif_offset
1540-
except AttributeError:
1541-
pass
1538+
thumbnail_offset += getattr(self, "_exif_offset", 0)
15421539
self.fp.seek(thumbnail_offset)
15431540
data = self.fp.read(ifd.get(514))
15441541
fp = io.BytesIO(data)
@@ -1604,7 +1601,7 @@ def has_transparency_data(self) -> bool:
16041601
or "transparency" in self.info
16051602
)
16061603

1607-
def apply_transparency(self):
1604+
def apply_transparency(self) -> None:
16081605
"""
16091606
If a P mode image has a "transparency" key in the info dictionary,
16101607
remove the key and instead apply the transparency to the palette.
@@ -1616,6 +1613,7 @@ def apply_transparency(self):
16161613
from . import ImagePalette
16171614

16181615
palette = self.getpalette("RGBA")
1616+
assert palette is not None
16191617
transparency = self.info["transparency"]
16201618
if isinstance(transparency, bytes):
16211619
for i, alpha in enumerate(transparency):

src/PIL/ImageDraw.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,13 @@ def getdraw(im=None, hints=None):
908908
return im, handler
909909

910910

911-
def floodfill(image: Image.Image, xy, value, border=None, thresh=0) -> None:
911+
def floodfill(
912+
image: Image.Image,
913+
xy: tuple[int, int],
914+
value: float | tuple[int, ...],
915+
border: float | tuple[int, ...] | None = None,
916+
thresh: float = 0,
917+
) -> None:
912918
"""
913919
(experimental) Fills a bounded region with a given color.
914920

src/PIL/PyAccess.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import logging
2424
import sys
25+
from typing import TYPE_CHECKING
2526

2627
from ._deprecate import deprecate
2728

@@ -48,9 +49,12 @@
4849

4950
logger = logging.getLogger(__name__)
5051

52+
if TYPE_CHECKING:
53+
from . import Image
54+
5155

5256
class PyAccess:
53-
def __init__(self, img, readonly=False):
57+
def __init__(self, img: Image.Image, readonly: bool = False) -> None:
5458
deprecate("PyAccess", 11)
5559
vals = dict(img.im.unsafe_ptrs)
5660
self.readonly = readonly
@@ -77,7 +81,8 @@ def __setitem__(self, xy, color):
7781
"""
7882
Modifies the pixel at x,y. The color is given as a single
7983
numerical value for single band images, and a tuple for
80-
multi-band images
84+
multi-band images. In addition to this, RGB and RGBA tuples
85+
are accepted for P and PA images.
8186
8287
:param xy: The pixel coordinate, given as (x, y). See
8388
:ref:`coordinate-system`.
@@ -108,7 +113,7 @@ def __setitem__(self, xy, color):
108113

109114
return self.set_pixel(x, y, color)
110115

111-
def __getitem__(self, xy):
116+
def __getitem__(self, xy: tuple[int, int]) -> float | tuple[int, ...]:
112117
"""
113118
Returns the pixel at x,y. The pixel is returned as a single
114119
value for single band images or a tuple for multiple band
@@ -130,21 +135,27 @@ def __getitem__(self, xy):
130135
putpixel = __setitem__
131136
getpixel = __getitem__
132137

133-
def check_xy(self, xy):
138+
def check_xy(self, xy: tuple[int, int]) -> tuple[int, int]:
134139
(x, y) = xy
135140
if not (0 <= x < self.xsize and 0 <= y < self.ysize):
136141
msg = "pixel location out of range"
137142
raise ValueError(msg)
138143
return xy
139144

145+
def get_pixel(self, x: int, y: int) -> float | tuple[int, ...]:
146+
raise NotImplementedError()
147+
148+
def set_pixel(self, x: int, y: int, color: float | tuple[int, ...]) -> None:
149+
raise NotImplementedError()
150+
140151

141152
class _PyAccess32_2(PyAccess):
142153
"""PA, LA, stored in first and last bytes of a 32 bit word"""
143154

144155
def _post_init(self, *args, **kwargs):
145156
self.pixels = ffi.cast("struct Pixel_RGBA **", self.image32)
146157

147-
def get_pixel(self, x, y):
158+
def get_pixel(self, x: int, y: int) -> tuple[int, int]:
148159
pixel = self.pixels[y][x]
149160
return pixel.r, pixel.a
150161

@@ -161,7 +172,7 @@ class _PyAccess32_3(PyAccess):
161172
def _post_init(self, *args, **kwargs):
162173
self.pixels = ffi.cast("struct Pixel_RGBA **", self.image32)
163174

164-
def get_pixel(self, x, y):
175+
def get_pixel(self, x: int, y: int) -> tuple[int, int, int]:
165176
pixel = self.pixels[y][x]
166177
return pixel.r, pixel.g, pixel.b
167178

@@ -180,7 +191,7 @@ class _PyAccess32_4(PyAccess):
180191
def _post_init(self, *args, **kwargs):
181192
self.pixels = ffi.cast("struct Pixel_RGBA **", self.image32)
182193

183-
def get_pixel(self, x, y):
194+
def get_pixel(self, x: int, y: int) -> tuple[int, int, int, int]:
184195
pixel = self.pixels[y][x]
185196
return pixel.r, pixel.g, pixel.b, pixel.a
186197

@@ -199,7 +210,7 @@ class _PyAccess8(PyAccess):
199210
def _post_init(self, *args, **kwargs):
200211
self.pixels = self.image8
201212

202-
def get_pixel(self, x, y):
213+
def get_pixel(self, x: int, y: int) -> int:
203214
return self.pixels[y][x]
204215

205216
def set_pixel(self, x, y, color):
@@ -217,7 +228,7 @@ class _PyAccessI16_N(PyAccess):
217228
def _post_init(self, *args, **kwargs):
218229
self.pixels = ffi.cast("unsigned short **", self.image)
219230

220-
def get_pixel(self, x, y):
231+
def get_pixel(self, x: int, y: int) -> int:
221232
return self.pixels[y][x]
222233

223234
def set_pixel(self, x, y, color):
@@ -235,7 +246,7 @@ class _PyAccessI16_L(PyAccess):
235246
def _post_init(self, *args, **kwargs):
236247
self.pixels = ffi.cast("struct Pixel_I16 **", self.image)
237248

238-
def get_pixel(self, x, y):
249+
def get_pixel(self, x: int, y: int) -> int:
239250
pixel = self.pixels[y][x]
240251
return pixel.l + pixel.r * 256
241252

@@ -256,7 +267,7 @@ class _PyAccessI16_B(PyAccess):
256267
def _post_init(self, *args, **kwargs):
257268
self.pixels = ffi.cast("struct Pixel_I16 **", self.image)
258269

259-
def get_pixel(self, x, y):
270+
def get_pixel(self, x: int, y: int) -> int:
260271
pixel = self.pixels[y][x]
261272
return pixel.l * 256 + pixel.r
262273

@@ -277,7 +288,7 @@ class _PyAccessI32_N(PyAccess):
277288
def _post_init(self, *args, **kwargs):
278289
self.pixels = self.image32
279290

280-
def get_pixel(self, x, y):
291+
def get_pixel(self, x: int, y: int) -> int:
281292
return self.pixels[y][x]
282293

283294
def set_pixel(self, x, y, color):
@@ -296,7 +307,7 @@ def reverse(self, i):
296307
chars[0], chars[1], chars[2], chars[3] = chars[3], chars[2], chars[1], chars[0]
297308
return ffi.cast("int *", chars)[0]
298309

299-
def get_pixel(self, x, y):
310+
def get_pixel(self, x: int, y: int) -> int:
300311
return self.reverse(self.pixels[y][x])
301312

302313
def set_pixel(self, x, y, color):
@@ -309,7 +320,7 @@ class _PyAccessF(PyAccess):
309320
def _post_init(self, *args, **kwargs):
310321
self.pixels = ffi.cast("float **", self.image32)
311322

312-
def get_pixel(self, x, y):
323+
def get_pixel(self, x: int, y: int) -> float:
313324
return self.pixels[y][x]
314325

315326
def set_pixel(self, x, y, color):
@@ -357,7 +368,7 @@ def set_pixel(self, x, y, color):
357368
mode_map["I;32B"] = _PyAccessI32_N
358369

359370

360-
def new(img, readonly=False):
371+
def new(img: Image.Image, readonly: bool = False) -> PyAccess | None:
361372
access_type = mode_map.get(img.mode, None)
362373
if not access_type:
363374
logger.debug("PyAccess Not Implemented: %s", img.mode)

0 commit comments

Comments
 (0)