Skip to content

Commit 76f8be6

Browse files
committed
Added type hints
1 parent 4d9f78e commit 76f8be6

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

Tests/test_imageops.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ def test_autocontrast_preserve_one_color(color: tuple[int, int, int]) -> None:
606606
assert_image_equal(img, out)
607607

608608

609-
def test_dither_primary_returns_image():
609+
def test_dither_primary_returns_image() -> None:
610610
im = Image.new("RGB", (4, 4), (128, 128, 128))
611611
out = ImageOps.dither_primary(im)
612612

@@ -615,7 +615,7 @@ def test_dither_primary_returns_image():
615615
assert out.mode == "RGB"
616616

617617

618-
def test_dither_primary_uses_only_primary_colors():
618+
def test_dither_primary_uses_only_primary_colors() -> None:
619619
im = Image.new("RGB", (4, 4), (200, 100, 50))
620620
out = ImageOps.dither_primary(im)
621621

@@ -628,7 +628,7 @@ def test_dither_primary_uses_only_primary_colors():
628628
assert b in (0, 255)
629629

630630

631-
def test_dither_primary_small_image():
631+
def test_dither_primary_small_image() -> None:
632632
im = Image.new("RGB", (2, 2), (255, 0, 0))
633633
out = ImageOps.dither_primary(im)
634634

src/PIL/ImageOps.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -675,35 +675,43 @@ def dither_primary(image: Image.Image) -> Image.Image:
675675
dst = out.load()
676676

677677
# Step 1: primary color reduction
678+
assert src is not None
678679
for x in range(width):
679680
for y in range(height):
680-
r, g, b = src[x, y]
681+
px = src[x, y]
682+
assert isinstance(px, tuple)
683+
r, g, b = px
681684
src[x, y] = (
682685
255 if r > 127 else 0,
683686
255 if g > 127 else 0,
684687
255 if b > 127 else 0,
685688
)
686689

687690
# Step 2: ordered dithering (2x2 blocks)
691+
assert dst is not None
688692
for x in range(0, width - 1, 2):
689693
for y in range(0, height - 1, 2):
690694
p1 = src[x, y]
691695
p2 = src[x, y + 1]
692696
p3 = src[x + 1, y]
693697
p4 = src[x + 1, y + 1]
694698

699+
assert isinstance(p1, tuple)
700+
assert isinstance(p2, tuple)
701+
assert isinstance(p3, tuple)
702+
assert isinstance(p4, tuple)
695703
red = (p1[0] + p2[0] + p3[0] + p4[0]) / 4
696704
green = (p1[1] + p2[1] + p3[1] + p4[1]) / 4
697705
blue = (p1[2] + p2[2] + p3[2] + p4[2]) / 4
698706

699-
r = [_dither_saturation(red, q) for q in range(4)]
700-
g = [_dither_saturation(green, q) for q in range(4)]
701-
b = [_dither_saturation(blue, q) for q in range(4)]
707+
r1 = [_dither_saturation(red, q) for q in range(4)]
708+
g1 = [_dither_saturation(green, q) for q in range(4)]
709+
b1 = [_dither_saturation(blue, q) for q in range(4)]
702710

703-
dst[x, y] = (r[0], g[0], b[0])
704-
dst[x, y + 1] = (r[1], g[1], b[1])
705-
dst[x + 1, y] = (r[2], g[2], b[2])
706-
dst[x + 1, y + 1] = (r[3], g[3], b[3])
711+
dst[x, y] = (r1[0], g1[0], b1[0])
712+
dst[x, y + 1] = (r1[1], g1[1], b1[1])
713+
dst[x + 1, y] = (r1[2], g1[2], b1[2])
714+
dst[x + 1, y + 1] = (r1[3], g1[3], b1[3])
707715

708716
return out
709717

0 commit comments

Comments
 (0)