#!/usr/bin/env python3 """Finds the star images in a photograph: points that stand out from a smoothed background. Needs Pillow. python3 -I -X utf8 star_points.py IMAGE OUT_TSV [THRESHOLD] Writes x, y (centroid, pixels from the top left), the number of pixels and the peak brightness of each point, brightest first. The photograph is someone else's: it is read here, never hosted. Used once, for addendum G7 of RULES_photo_geometry.txt; the positions it found are written into photo_stars.py. """ import sys from PIL import Image, ImageFilter path, out = sys.argv[1], sys.argv[2] thr = float(sys.argv[3]) if len(sys.argv) > 3 else 18.0 im = Image.open(path).convert("L") w, h = im.size bg = im.filter(ImageFilter.GaussianBlur(5)) lp, bp = im.load(), bg.load() mask = [[(lp[x, y] - bp[x, y]) > thr for x in range(w)] for y in range(h)] seen = [[False] * w for _ in range(h)] blobs = [] for y in range(h): for x in range(w): if mask[y][x] and not seen[y][x]: stack, pts = [(x, y)], [] seen[y][x] = True while stack: cx, cy = stack.pop() pts.append((cx, cy)) for dx in (-1, 0, 1): for dy in (-1, 0, 1): nx, ny = cx + dx, cy + dy if 0 <= nx < w and 0 <= ny < h and mask[ny][nx] and not seen[ny][nx]: seen[ny][nx] = True stack.append((nx, ny)) if len(pts) > 60: continue # too large for a star: a label, a lamp, the trees ws = sum(lp[a, b] - bp[a, b] for a, b in pts) mx = sum(a * (lp[a, b] - bp[a, b]) for a, b in pts) / ws my = sum(b * (lp[a, b] - bp[a, b]) for a, b in pts) / ws blobs.append((ws, mx, my, len(pts), max(lp[a, b] for a, b in pts))) blobs.sort(reverse=True) with open(out, "w", encoding="utf-8", newline="\n") as f: f.write("# x\ty\tpixels\tpeak\tflux\n") for ws, mx, my, n, pk in blobs: f.write(f"{mx:.1f}\t{my:.1f}\t{n}\t{pk}\t{ws:.0f}\n") print(w, h, len(blobs), "points")