#!/usr/bin/env python3 """Measures the tree line of a night photograph: in columns across the frame, the last row of sky above the dark silhouette at the bottom. Needs Pillow. python3 -I -X utf8 tree_line.py IMAGE OUT_TSV [STEP] In each column (every STEP pixels, default 10) the smoothed brightness is followed downward from the top; the tree line is the row above the first run of 10 dark rows (darker than a fifth of the column's median sky). The photograph is someone else's: it is read here, never hosted. Used once, for addendum G7 of RULES_photo_geometry.txt. """ import statistics import sys from PIL import Image, ImageFilter path, out = sys.argv[1], sys.argv[2] step = int(sys.argv[3]) if len(sys.argv) > 3 else 10 im = Image.open(path).convert("L").filter(ImageFilter.MedianFilter(5)) w, h = im.size px = im.load() rows = [] for x in range(step, w - step + 1, step): col = [px[x, y] for y in range(h)] sky = statistics.median(col[: h // 2]) dark = max(6.0, sky / 5.0) y_tree = None for y in range(h // 3, h - 10): if all(v < dark for v in col[y: y + 10]): y_tree = y - 1 break rows.append((x, y_tree, sky, dark)) with open(out, "w", encoding="utf-8", newline="\n") as f: f.write("# x\ty_last_sky_row\tmedian_sky\tdark_threshold\n") for x, y, sky, dark in rows: f.write(f"{x}\t{'' if y is None else y}\t{sky:.0f}\t{dark:.1f}\n") print(len(rows), "columns;", sum(1 for r in rows if r[1] is None), "with no tree line found")