2222
2323import logging
2424import sys
25+ from typing import TYPE_CHECKING
2526
2627from ._deprecate import deprecate
2728
4849
4950logger = logging .getLogger (__name__ )
5051
52+ if TYPE_CHECKING :
53+ from . import Image
54+
5155
5256class 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
141152class _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