Move the grid row finder into the package
This commit is contained in:
1 parent
42f042eb95
commit
a3a87d1b74
1 file changed
+1
-1
@@ -0,0 +1,160 @@
|
||||
"""Find the week rows of a calendar or menu grid so each row can be read on its own.
|
||||
|
||||
Pillow only. Run directly to print the rows found in each grid page and save the strips:
|
||||
python -m parentcalendar.grid <image> [out_dir]
|
||||
"""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from PIL import Image, ImageChops, ImageFilter, ImageOps
|
||||
|
||||
|
||||
def line_mask(gray):
|
||||
"""White where a pixel is clearly darker than its surroundings (lines, text), black elsewhere.
|
||||
Filled areas such as banners have no local contrast inside, so they drop out."""
|
||||
local = gray.filter(ImageFilter.BoxBlur(12))
|
||||
return ImageChops.subtract(local, gray).point(lambda v: 255 if v > 20 else 0)
|
||||
|
||||
|
||||
def row_means(img):
|
||||
return list(img.resize((1, img.height), Image.BOX).get_flattened_data())
|
||||
|
||||
|
||||
def deskew(img, max_angle=5.0, step=0.25):
|
||||
"""Rotate so lines are level: pick the angle whose row profile is sharpest. Returns (image, angle)."""
|
||||
small = ImageOps.grayscale(img)
|
||||
small.thumbnail((700, 700))
|
||||
mask = line_mask(small)
|
||||
|
||||
def sharpness(angle):
|
||||
rows = row_means(mask.rotate(angle, resample=Image.BILINEAR))
|
||||
mean = sum(rows) / len(rows)
|
||||
return sum((r - mean) ** 2 for r in rows)
|
||||
|
||||
n = int(max_angle / step)
|
||||
best = max((i * step for i in range(-n, n + 1)), key=sharpness)
|
||||
if best == 0:
|
||||
return img, 0.0
|
||||
return img.rotate(best, resample=Image.BICUBIC, fillcolor="white"), best
|
||||
|
||||
|
||||
def coverage_profile(img, window=10, chunks=120):
|
||||
"""For each y: the share of the page's middle that has a dark pixel within +/- window rows.
|
||||
The window tolerates the slight slant left after deskewing a phone photo."""
|
||||
gray = ImageOps.grayscale(img)
|
||||
w, h = gray.size
|
||||
band = line_mask(gray).crop((int(w * 0.1), 0, int(w * 0.9), h)).resize((chunks, h), Image.BOX)
|
||||
px = band.load()
|
||||
prefix = [] # per chunk: running count of dark rows
|
||||
for x in range(chunks):
|
||||
run, p = 0, [0]
|
||||
for y in range(h):
|
||||
run += px[x, y] > 0
|
||||
p.append(run)
|
||||
prefix.append(p)
|
||||
return [sum(p[min(h, y + window + 1)] > p[max(0, y - window)] for p in prefix) / chunks for y in range(h)]
|
||||
|
||||
|
||||
def find_lines(profile, coverage=0.9, window=10):
|
||||
"""y positions of thin horizontal lines that cross (nearly) the whole middle of the page."""
|
||||
covered = [c >= coverage for c in profile]
|
||||
lines, start = [], None
|
||||
for y, c in enumerate(covered + [False]):
|
||||
if c and start is None:
|
||||
start = y
|
||||
elif not c and start is not None:
|
||||
if y - start <= 2 * window + 25: # thin: a line, not a textured banner
|
||||
lines.append((start + y) // 2)
|
||||
start = None
|
||||
return lines
|
||||
|
||||
|
||||
def tighten_header(img, top, bottom, wide=0.15, pad=6):
|
||||
"""Keep only the line of weekday names just above the grid. Notes printed above a column
|
||||
(like "2hr Early Out" over FRI) cover too little of the width to count, so they are cut off
|
||||
instead of showing up in every row's strip."""
|
||||
inked = [c >= wide for c in coverage_profile(img.crop((0, top, img.width, bottom)), window=0)]
|
||||
y = len(inked) - 1
|
||||
while y >= 0 and inked[y]: # the grid's top line
|
||||
y -= 1
|
||||
while y >= 0 and not inked[y]: # space between the names and the line
|
||||
y -= 1
|
||||
if y < 0:
|
||||
return top, bottom
|
||||
while y >= 0 and inked[y]: # the weekday names
|
||||
y -= 1
|
||||
return max(top, top + y + 1 - pad), bottom
|
||||
|
||||
|
||||
def find_rows(img):
|
||||
"""Returns (rows, header): rows are (top, bottom) pixel bands of the week rows, header is the
|
||||
band with the column headings above them. rows is empty when no grid is found."""
|
||||
h, w = img.height, img.width
|
||||
profile = coverage_profile(img)
|
||||
lines = find_lines(profile)
|
||||
gaps = [(a, b) for a, b in zip(lines, lines[1:])]
|
||||
if not gaps:
|
||||
return [], None
|
||||
mask = line_mask(ImageOps.grayscale(img)).crop((int(w * 0.1), 0, int(w * 0.9), h))
|
||||
|
||||
def density(a, b): # banners and clip art are dense; rows of text are not
|
||||
return mask.crop((0, a, mask.width, b)).resize((1, 1), Image.BOX).getpixel((0, 0)) / 255
|
||||
|
||||
tallest = max(b - a for a, b in gaps)
|
||||
candidates = [(a, b) for a, b in gaps if b - a >= 0.5 * tallest and density(a, b) < 0.18]
|
||||
if not candidates:
|
||||
return [], None
|
||||
median = sorted(b - a for a, b in candidates)[len(candidates) // 2]
|
||||
# Walk down from the first full-height row, taking the next line about one row-height below.
|
||||
# Lines in between (a long arrow, a banner edge) are skipped rather than splitting a row.
|
||||
# When no strong line is there (a slanted bottom edge in a photo), accept a weaker one.
|
||||
current, rows = candidates[0][0], []
|
||||
while True:
|
||||
nxt = [y for y in lines if 0.75 * median <= y - current <= 1.3 * median]
|
||||
if nxt:
|
||||
best = min(nxt, key=lambda y: abs(y - current - median))
|
||||
else:
|
||||
lo, hi = int(current + 0.8 * median), int(current + 1.2 * median)
|
||||
if hi > h: # no room for another full row: what's left is spill-over text
|
||||
break
|
||||
best = max(range(lo, hi), key=lambda y: profile[y])
|
||||
if profile[best] < 0.6:
|
||||
break
|
||||
rows.append((current, best))
|
||||
current = best
|
||||
if len(rows) < 2:
|
||||
return [], None
|
||||
above = [(a, b) for a, b in gaps if b == rows[0][0]]
|
||||
if above and 0.12 * median <= above[0][1] - above[0][0] < 0.5 * tallest:
|
||||
header = above[0] # boxed heading row
|
||||
else:
|
||||
header = tighten_header(img, max(0, rows[0][0] - median // 2), rows[0][0]) # headings printed above the grid
|
||||
padded = [(max(0, a - median * 3 // 100), min(h, b + median * 8 // 100)) for a, b in rows]
|
||||
a, _ = padded[-1]
|
||||
padded[-1] = (a, min(h, rows[-1][1] + median // 2)) # text often spills below the last row
|
||||
return padded, header
|
||||
|
||||
|
||||
def strip(img, header, row):
|
||||
"""Column headings stacked on top of one week row."""
|
||||
w = img.width
|
||||
top = img.crop((0, header[0], w, header[1]))
|
||||
body = img.crop((0, row[0], w, row[1]))
|
||||
out = Image.new("RGB", (w, top.height + body.height), "white")
|
||||
out.paste(top, (0, 0))
|
||||
out.paste(body, (0, top.height))
|
||||
return out
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
path = Path(sys.argv[1])
|
||||
img = ImageOps.exif_transpose(Image.open(path)).convert("RGB")
|
||||
img.thumbnail((2000, 2000))
|
||||
img, angle = deskew(img)
|
||||
rows, header = find_rows(img)
|
||||
print(f"{path.name}: size {img.size}, deskew {angle} deg, header {header}, rows {rows}")
|
||||
if len(sys.argv) > 2:
|
||||
out = Path(sys.argv[2])
|
||||
out.mkdir(parents=True, exist_ok=True)
|
||||
for i, row in enumerate(rows, 1):
|
||||
strip(img, header, row).save(out / f"{path.stem}-row{i}.jpg", quality=85)
|
||||
Reference in new issue
Block a user