Skip to content

Commit dfb0e5e

Browse files
committed
Tell mypy what get_flattened_data returns for an RGB image
Pillow's stub types get_flattened_data() for every mode at once, so the switch from getdata() in 329cea3 failed the typing contract. The image is converted to RGB first, so the pixels are int triples; cast says so.
1 parent d98911a commit dfb0e5e

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

‎je_auto_control/utils/color_stats/color_stats.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from dataclasses import asdict, dataclass
1515
from pathlib import Path
1616
from typing import (
17-
TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Tuple, Union,
17+
TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Tuple, Union, cast,
1818
)
1919

2020
if TYPE_CHECKING: # pragma: no cover - annotations only
@@ -81,7 +81,9 @@ def region_color_stats(source: ImageSource,
8181
left, top, right, bottom = (int(v) for v in region)
8282
image = image.crop((left, top, right, bottom))
8383
image.thumbnail((128, 128))
84-
pixels: List[RGB] = list(image.get_flattened_data())
84+
# _load_rgb converted the image, so every pixel is an (r, g, b) int
85+
# triple; Pillow's stub types the result for every mode at once.
86+
pixels = cast(List[RGB], list(image.get_flattened_data()))
8587
count = len(pixels)
8688
if count == 0:
8789
return ColorStats((0, 0, 0), (0, 0, 0), 0.0, 0)

0 commit comments

Comments
 (0)