Claude clean up

This commit is contained in:
Andras Schmelczer 2026-05-03 11:06:19 +01:00
parent eed1567f7f
commit 8609b4a884
11 changed files with 48 additions and 63 deletions

View file

@ -144,6 +144,9 @@ class ImmichClient:
return assets
_ROTATED_ORIENTATIONS = {5, 6, 7, 8, "5", "6", "7", "8"}
def _filter_by_orientation(assets: list[dict], portrait: bool) -> list[dict]:
"""Keep assets matching the requested orientation. Skips assets without EXIF dimensions."""
out = []
@ -153,7 +156,7 @@ def _filter_by_orientation(assets: list[dict], portrait: bool) -> list[dict]:
h = exif.get("exifImageHeight") or 0
if not (w and h):
continue
if exif.get("orientation") in (6, 8, "6", "8"):
if exif.get("orientation") in _ROTATED_ORIENTATIONS:
w, h = h, w
if (h > w) == portrait:
out.append(a)
@ -166,7 +169,7 @@ def _on_this_day_candidates(assets: list[dict]) -> tuple[list[dict], bool]:
Returns (candidates, is_exact). `is_exact` is True when same-month-day matches
exist; callers use it to weight the pool higher than the looser ±3-day fallback.
"""
today = datetime.now(UTC).date()
today = datetime.now().date()
dated = []
for a in assets:
exif = a.get("exifInfo") or {}

View file

@ -9,7 +9,8 @@ def urlopen_with_retry(req: Request, timeout: int = 30):
for delay in (3, 10, None):
try:
return urlopen(req, timeout=timeout)
except (URLError, TimeoutError):
except (URLError, TimeoutError) as e:
if delay is None:
raise
print(f"urlopen {req.full_url}: {e}; retry in {delay}s")
time.sleep(delay)

View file

@ -55,6 +55,7 @@ def format_age(asset: dict) -> str | None:
if count == 1:
return f"A {unit} ago"
return f"{count} {unit}s ago"
return None
def format_location(asset: dict) -> str | None:

View file

@ -1,11 +1,14 @@
"""Simple terminal progress bar for e-ink frame."""
import sys
class ProgressBar:
def __init__(self, total: int, desc: str = ""):
self.total = total
self.desc = desc
self._last_percent = -1
self._is_tty = sys.stdout.isatty()
def set(self, value: int) -> None:
if self.total == 0:
@ -14,10 +17,16 @@ class ProgressBar:
percent = int(100 * value / self.total)
if percent == self._last_percent:
return
# In non-tty (cron log) mode, only emit a few milestones — no \r spam.
if not self._is_tty and percent not in (25, 50, 75, 100):
return
self._last_percent = percent
filled = int(30 * value / self.total)
bar = "" * filled + "" * (30 - filled)
end = "\n" if value >= self.total else ""
prefix = f"{self.desc}: " if self.desc else ""
print(f"\r{prefix}|{bar}| {percent:3d}%", end=end, flush=True)
if self._is_tty:
filled = int(30 * value / self.total)
bar = "" * filled + "" * (30 - filled)
end = "\n" if value >= self.total else ""
print(f"\r{prefix}|{bar}| {percent:3d}%", end=end, flush=True)
else:
print(f"{prefix}{percent}%", flush=True)

View file

@ -2,9 +2,9 @@
# Waveshare 7.3" 6-color e-Paper driver (modified)
import numpy as np
import cv2
from PIL import Image, ImageEnhance
from numba import jit
from crop import face_aware_crop
from progress import ProgressBar
from overlay import render_text_into_indices
from . import epdconfig
@ -13,13 +13,13 @@ EPD_WIDTH = 800
EPD_HEIGHT = 480
# 6-color e-ink encoding: indices 0,1,2,3,5,6 are wire-format colors;
# 4 is reserved/unused (filled with BLACK so nearest-color never picks it).
# 4 is reserved/unused — _find_nearest_color skips it explicitly.
PALETTE_RGB = np.array([
[0, 0, 0], # 0: BLACK
[255, 255, 255], # 1: WHITE
[255, 255, 0], # 2: YELLOW
[255, 0, 0], # 3: RED
[0, 0, 0], # 4: unused
[0, 0, 0], # 4: unused (skipped)
[0, 0, 255], # 5: BLUE
[0, 255, 0], # 6: GREEN
], dtype=np.float64)
@ -56,28 +56,12 @@ def _enhance_for_eink(image: Image.Image, saturation: float,
return img
def _crop_center(image: Image.Image, target_w: int, target_h: int) -> Image.Image:
print("Center cropping...")
img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
img_h, img_w = img_cv.shape[:2]
img_aspect, target_aspect = img_w / img_h, target_w / target_h
if img_aspect < target_aspect:
new_w, new_h = target_w, int(target_w / img_aspect)
else:
new_w, new_h = int(target_h * img_aspect), target_h
img_cv = cv2.resize(img_cv, (new_w, new_h), interpolation=cv2.INTER_LANCZOS4)
x_off = (new_w - target_w) // 2
y_off = (new_h - target_h) // 2
cropped = img_cv[y_off:y_off + target_h, x_off:x_off + target_w]
return Image.fromarray(cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB))
@jit(nopython=True, cache=True)
def _find_nearest_color(r, g, b, palette, weights):
best_idx, best_dist = 0, 1e10
for i in range(palette.shape[0]):
if i == 4: # reserved palette slot
continue
dr = (palette[i, 0] - r) * weights[0]
dg = (palette[i, 1] - g) * weights[1]
db = (palette[i, 2] - b) * weights[2]
@ -133,6 +117,8 @@ def _dither_atkinson(image: Image.Image) -> np.ndarray:
print("Dithering...")
progress = ProgressBar(height, desc="Dithering")
# Chunking is for progress reporting only; error diffusion still
# spans chunks because `img` is the same buffer between calls.
chunk_size = 48
for i in range((height + chunk_size - 1) // chunk_size):
start, end = i * chunk_size, min((i + 1) * chunk_size, height)
@ -208,7 +194,7 @@ class EPD:
image = image.convert('RGB')
if image.size != (self.width, self.height):
print(f"Input: {image.size[0]}x{image.size[1]}{self.width}x{self.height}")
image = _crop_center(image, self.width, self.height)
image = face_aware_crop(image, self.width, self.height, [])
print("Enhancing...")
image = _enhance_for_eink(image, saturation, contrast, gamma)
@ -221,7 +207,7 @@ class EPD:
print("Packing buffer...")
flat = indices.reshape(-1)
return ((flat[0::2].astype(np.uint8) << 4) | flat[1::2].astype(np.uint8)).tolist()
return bytes((flat[0::2] << 4) | flat[1::2])
def display(self, image):
self.send_command(0x10)

View file

@ -87,13 +87,13 @@ class RaspberryPi:
if pin == self.BUSY_PIN:
return self.GPIO_BUSY_PIN.value
elif pin == self.RST_PIN:
return self.RST_PIN.value
return self.GPIO_RST_PIN.value
elif pin == self.DC_PIN:
return self.DC_PIN.value
return self.GPIO_DC_PIN.value
# elif pin == self.CS_PIN:
# return self.CS_PIN.value
# return self.GPIO_CS_PIN.value
elif pin == self.PWR_PIN:
return self.PWR_PIN.value
return self.GPIO_PWR_PIN.value
def delay_ms(self, delaytime):
time.sleep(delaytime / 1000.0)
@ -134,7 +134,7 @@ class RaspberryPi:
self.DEV_SPI = CDLL(so_filename)
break
if self.DEV_SPI is None:
RuntimeError('Cannot find DEV_Config.so')
raise RuntimeError('Cannot find DEV_Config.so')
self.DEV_SPI.DEV_Module_Init()