You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.2 KiB

"""Ermittelt, welche Bildbereiche sich geändert haben (das Display ist langsam: ~2 s pro Vollbild)."""
from PIL import Image, ImageChops
BAND_HEIGHT = 16
Rect = tuple[int, int, int, int] # x, y, Breite, Höhe
def changed_rects(old: Image.Image | None, new: Image.Image) -> list[Rect]:
width, height = new.size
if old is None or old.size != new.size:
return [(0, 0, width, height)]
diff = ImageChops.difference(old.convert("RGB"), new.convert("RGB"))
boxes: list[list[int]] = [] # [x0, y0, x1, y1], x1/y1 exklusiv
current: list[int] | None = None
for top in range(0, height, BAND_HEIGHT):
bbox = diff.crop((0, top, width, min(top + BAND_HEIGHT, height))).getbbox()
if bbox is None:
if current is not None:
boxes.append(current)
current = None
continue
x0, y0, x1, y1 = bbox[0], top + bbox[1], bbox[2], top + bbox[3]
if current is None:
current = [x0, y0, x1, y1]
else:
current = [min(current[0], x0), current[1], max(current[2], x1), y1]
if current is not None:
boxes.append(current)
return [(x0, y0, x1 - x0, y1 - y0) for x0, y0, x1, y1 in boxes]