Skip to content

Commit 714d945

Browse files
committed
Added type hints to ImageFilter
1 parent 4b258be commit 714d945

File tree

4 files changed

+58
-35
lines changed

4 files changed

+58
-35
lines changed

Tests/test_color_lut.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,10 @@ def test_overflow(self) -> None:
354354
class TestColorLut3DFilter:
355355
def test_wrong_args(self) -> None:
356356
with pytest.raises(ValueError, match="should be either an integer"):
357-
ImageFilter.Color3DLUT("small", [1])
357+
ImageFilter.Color3DLUT("small", [1]) # type: ignore[arg-type]
358358

359359
with pytest.raises(ValueError, match="should be either an integer"):
360-
ImageFilter.Color3DLUT((11, 11), [1])
360+
ImageFilter.Color3DLUT((11, 11), [1]) # type: ignore[arg-type]
361361

362362
with pytest.raises(ValueError, match=r"in \[2, 65\] range"):
363363
ImageFilter.Color3DLUT((11, 11, 1), [1])

Tests/test_image_filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def test_builtinfilter_p() -> None:
137137
builtin_filter = ImageFilter.BuiltinFilter()
138138

139139
with pytest.raises(ValueError):
140-
builtin_filter.filter(hopper("P"))
140+
builtin_filter.filter(hopper("P").im)
141141

142142

143143
def test_kernel_not_enough_coefficients() -> None:

Tests/test_numpy.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,15 @@
1111

1212
if TYPE_CHECKING:
1313
import numpy
14-
import numpy.typing
14+
import numpy.typing as npt
1515
else:
1616
numpy = pytest.importorskip("numpy", reason="NumPy not installed")
1717

1818
TEST_IMAGE_SIZE = (10, 10)
1919

2020

2121
def test_numpy_to_image() -> None:
22-
def to_image(
23-
dtype: numpy.typing.DTypeLike, bands: int = 1, boolean: int = 0
24-
) -> Image.Image:
22+
def to_image(dtype: npt.DTypeLike, bands: int = 1, boolean: int = 0) -> Image.Image:
2523
if bands == 1:
2624
if boolean:
2725
data = [0, 255] * 50
@@ -106,9 +104,7 @@ def test_1d_array() -> None:
106104
assert_image(Image.fromarray(a), "L", (1, 5))
107105

108106

109-
def _test_img_equals_nparray(
110-
img: Image.Image, np_img: numpy.typing.NDArray[Any]
111-
) -> None:
107+
def _test_img_equals_nparray(img: Image.Image, np_img: npt.NDArray[Any]) -> None:
112108
assert len(np_img.shape) >= 2
113109
np_size = np_img.shape[1], np_img.shape[0]
114110
assert img.size == np_size
@@ -166,7 +162,7 @@ def test_save_tiff_uint16() -> None:
166162
("HSV", numpy.uint8),
167163
),
168164
)
169-
def test_to_array(mode: str, dtype: numpy.typing.DTypeLike) -> None:
165+
def test_to_array(mode: str, dtype: npt.DTypeLike) -> None:
170166
img = hopper(mode)
171167

172168
# Resize to non-square
@@ -216,7 +212,7 @@ def test_putdata() -> None:
216212
numpy.float64,
217213
),
218214
)
219-
def test_roundtrip_eye(dtype: numpy.typing.DTypeLike) -> None:
215+
def test_roundtrip_eye(dtype: npt.DTypeLike) -> None:
220216
arr = numpy.eye(10, dtype=dtype)
221217
numpy.testing.assert_array_equal(arr, numpy.array(Image.fromarray(arr)))
222218

src/PIL/ImageFilter.py

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@
1919
import abc
2020
import functools
2121
from types import ModuleType
22-
from typing import Any, Sequence
22+
from typing import TYPE_CHECKING, Any, Callable, Sequence, cast
23+
24+
if TYPE_CHECKING:
25+
import numpy.typing as npt
26+
27+
from . import _imaging
2328

2429

2530
class Filter:
2631
@abc.abstractmethod
27-
def filter(self, image):
32+
def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
2833
pass
2934

3035

@@ -33,7 +38,9 @@ class MultibandFilter(Filter):
3338

3439

3540
class BuiltinFilter(MultibandFilter):
36-
def filter(self, image):
41+
filterargs: tuple[Any, ...]
42+
43+
def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
3744
if image.mode == "P":
3845
msg = "cannot filter palette images"
3946
raise ValueError(msg)
@@ -91,7 +98,7 @@ def __init__(self, size: int, rank: int) -> None:
9198
self.size = size
9299
self.rank = rank
93100

94-
def filter(self, image):
101+
def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
95102
if image.mode == "P":
96103
msg = "cannot filter palette images"
97104
raise ValueError(msg)
@@ -158,7 +165,7 @@ class ModeFilter(Filter):
158165
def __init__(self, size: int = 3) -> None:
159166
self.size = size
160167

161-
def filter(self, image):
168+
def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
162169
return image.modefilter(self.size)
163170

164171

@@ -176,9 +183,9 @@ class GaussianBlur(MultibandFilter):
176183
def __init__(self, radius: float | Sequence[float] = 2) -> None:
177184
self.radius = radius
178185

179-
def filter(self, image):
186+
def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
180187
xy = self.radius
181-
if not isinstance(xy, (tuple, list)):
188+
if isinstance(xy, (int, float)):
182189
xy = (xy, xy)
183190
if xy == (0, 0):
184191
return image.copy()
@@ -208,9 +215,9 @@ def __init__(self, radius: float | Sequence[float]) -> None:
208215
raise ValueError(msg)
209216
self.radius = radius
210217

211-
def filter(self, image):
218+
def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
212219
xy = self.radius
213-
if not isinstance(xy, (tuple, list)):
220+
if isinstance(xy, (int, float)):
214221
xy = (xy, xy)
215222
if xy == (0, 0):
216223
return image.copy()
@@ -241,7 +248,7 @@ def __init__(
241248
self.percent = percent
242249
self.threshold = threshold
243250

244-
def filter(self, image):
251+
def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
245252
return image.unsharp_mask(self.radius, self.percent, self.threshold)
246253

247254

@@ -387,8 +394,13 @@ class Color3DLUT(MultibandFilter):
387394
name = "Color 3D LUT"
388395

389396
def __init__(
390-
self, size, table, channels: int = 3, target_mode: str | None = None, **kwargs
391-
):
397+
self,
398+
size: int | tuple[int, int, int],
399+
table,
400+
channels: int = 3,
401+
target_mode: str | None = None,
402+
**kwargs: bool,
403+
) -> None:
392404
if channels not in (3, 4):
393405
msg = "Only 3 or 4 output channels are supported"
394406
raise ValueError(msg)
@@ -410,15 +422,16 @@ def __init__(
410422
pass
411423

412424
if numpy and isinstance(table, numpy.ndarray):
425+
numpy_table: npt.NDArray[Any] = table
413426
if copy_table:
414-
table = table.copy()
427+
numpy_table = numpy_table.copy()
415428

416-
if table.shape in [
429+
if numpy_table.shape in [
417430
(items * channels,),
418431
(items, channels),
419432
(size[2], size[1], size[0], channels),
420433
]:
421-
table = table.reshape(items * channels)
434+
table = numpy_table.reshape(items * channels)
422435
else:
423436
wrong_size = True
424437

@@ -428,15 +441,17 @@ def __init__(
428441

429442
# Convert to a flat list
430443
if table and isinstance(table[0], (list, tuple)):
431-
table, raw_table = [], table
444+
raw_table = cast(Sequence[Sequence[int]], table)
445+
flat_table: list[int] = []
432446
for pixel in raw_table:
433447
if len(pixel) != channels:
434448
msg = (
435449
"The elements of the table should "
436450
f"have a length of {channels}."
437451
)
438452
raise ValueError(msg)
439-
table.extend(pixel)
453+
flat_table.extend(pixel)
454+
table = flat_table
440455

441456
if wrong_size or len(table) != items * channels:
442457
msg = (
@@ -449,23 +464,29 @@ def __init__(
449464
self.table = table
450465

451466
@staticmethod
452-
def _check_size(size: Any) -> list[int]:
467+
def _check_size(size: Any) -> tuple[int, int, int]:
453468
try:
454469
_, _, _ = size
455470
except ValueError as e:
456471
msg = "Size should be either an integer or a tuple of three integers."
457472
raise ValueError(msg) from e
458473
except TypeError:
459474
size = (size, size, size)
460-
size = [int(x) for x in size]
475+
size = tuple(int(x) for x in size)
461476
for size_1d in size:
462477
if not 2 <= size_1d <= 65:
463478
msg = "Size should be in [2, 65] range."
464479
raise ValueError(msg)
465480
return size
466481

467482
@classmethod
468-
def generate(cls, size, callback, channels=3, target_mode=None):
483+
def generate(
484+
cls,
485+
size: int | tuple[int, int, int],
486+
callback: Callable[[float, float, float], tuple[float, ...]],
487+
channels: int = 3,
488+
target_mode: str | None = None,
489+
) -> Color3DLUT:
469490
"""Generates new LUT using provided callback.
470491
471492
:param size: Size of the table. Passed to the constructor.
@@ -482,7 +503,7 @@ def generate(cls, size, callback, channels=3, target_mode=None):
482503
msg = "Only 3 or 4 output channels are supported"
483504
raise ValueError(msg)
484505

485-
table = [0] * (size_1d * size_2d * size_3d * channels)
506+
table: list[float] = [0] * (size_1d * size_2d * size_3d * channels)
486507
idx_out = 0
487508
for b in range(size_3d):
488509
for g in range(size_2d):
@@ -500,7 +521,13 @@ def generate(cls, size, callback, channels=3, target_mode=None):
500521
_copy_table=False,
501522
)
502523

503-
def transform(self, callback, with_normals=False, channels=None, target_mode=None):
524+
def transform(
525+
self,
526+
callback: Callable[..., tuple[float, ...]],
527+
with_normals: bool = False,
528+
channels: int | None = None,
529+
target_mode: str | None = None,
530+
) -> Color3DLUT:
504531
"""Transforms the table values using provided callback and returns
505532
a new LUT with altered values.
506533
@@ -564,7 +591,7 @@ def __repr__(self) -> str:
564591
r.append(f"target_mode={self.mode}")
565592
return "<{}>".format(" ".join(r))
566593

567-
def filter(self, image):
594+
def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
568595
from . import Image
569596

570597
return image.color_lut_3d(

0 commit comments

Comments
 (0)