Spaces:
Sleeping
Sleeping
Download core.py from prattel/handschrift: direct link, hf CLI and curl.
- Browser
- Download file 3.68 kB
-
https://huggingface.co/spaces/prattel/handschrift/resolve/main/core.py
- Command line
-
hf download hf://spaces/prattel/handschrift/core.py
-
curl -L -o core.py https://huggingface.co/spaces/prattel/handschrift/resolve/main/core.py
3.68 kB
| """Image preparation and request limits, independent of the ML runtime.""" | |
| from PIL import Image, ImageOps | |
| MAX_LINES = 12 | |
| MAX_LINE_CHARS = 80 | |
| MAX_TOTAL_CHARS = 600 | |
| def text_lines(text): | |
| lines = [line.strip() for line in (text or "").splitlines() if line.strip()] | |
| if not lines: | |
| raise ValueError("Bitte gib einen neuen Text ein.") | |
| if len(lines) > MAX_LINES: | |
| raise ValueError("Bitte höchstens zwölf Zeilen auf einmal erzeugen.") | |
| if any(len(line) > MAX_LINE_CHARS for line in lines): | |
| raise ValueError("Bitte lange Zeilen umbrechen: höchstens 80 Zeichen pro Zeile.") | |
| if sum(map(len, lines)) > MAX_TOTAL_CHARS: | |
| raise ValueError("Bitte höchstens 600 Zeichen auf einmal erzeugen.") | |
| return lines | |
| def white_background(image): | |
| image = ImageOps.exif_transpose(image) | |
| if image.mode in ("RGBA", "LA") or "transparency" in image.info: | |
| rgba = image.convert("RGBA") | |
| canvas = Image.new("RGBA", rgba.size, "white") | |
| image = Image.alpha_composite(canvas, rgba) | |
| return image.convert("RGB") | |
| def prepare_reference(editor_value): | |
| image = editor_value.get("composite") if isinstance(editor_value, dict) else editor_value | |
| if image is None: | |
| raise ValueError("Bitte lade eine Schriftprobe hoch und schneide eine einzelne Zeile aus.") | |
| if image.width * image.height > 20_000_000: | |
| raise ValueError("Das Bild ist zu groß. Bitte vorher auf unter 20 Megapixel verkleinern.") | |
| if min(image.size) < 16: | |
| raise ValueError("Der Ausschnitt ist zu klein. Bitte eine größere Schriftprobe verwenden.") | |
| gray = ImageOps.grayscale(white_background(image)) | |
| gray = ImageOps.autocontrast(gray, cutoff=1) | |
| if gray.getextrema()[1] - gray.getextrema()[0] < 20: | |
| raise ValueError("Keine deutliche Schrift erkannt. Bitte den Ausschnitt und Kontrast prüfen.") | |
| ratio = gray.width / gray.height | |
| if not 1.5 <= ratio <= 24: | |
| raise ValueError("Bitte eine einzelne waagerechte Textzeile ausschneiden (nicht die ganze Seite).") | |
| # Large empty margins make the model infer an already finished line. Trim | |
| # the outer paper while retaining all ink, including dots and descenders. | |
| ink = gray.point(lambda p: 255 if p < 245 else 0).getbbox() | |
| if ink: | |
| gray = gray.crop((max(0, ink[0]-4), max(0, ink[1]-4), | |
| min(gray.width, ink[2]+4), min(gray.height, ink[3]+4))) | |
| width = max(16, round(64 * gray.width / gray.height)) | |
| return gray.resize((width, 64), Image.Resampling.LANCZOS).convert("RGB") | |
| def compose_lines(images, black_and_white=False): | |
| if not images: | |
| raise ValueError("Das Modell hat kein Bild erzeugt. Bitte erneut versuchen.") | |
| cleaned = [] | |
| for image in images: | |
| gray = ImageOps.grayscale(white_background(image)) | |
| mask = gray.point(lambda p: 255 if p < 225 else 0) | |
| bbox = mask.getbbox() | |
| if bbox is None: | |
| raise ValueError("Das Modell hat eine leere Zeile erzeugt. Bitte kürzeren Text versuchen.") | |
| # Trim only horizontal empty space, preserving vertical alignment and letter size. | |
| gray = gray.crop((max(0, bbox[0] - 4), 0, min(gray.width, bbox[2] + 4), gray.height)) | |
| if black_and_white: | |
| gray = gray.point(lambda p: 0 if p < 160 else 255) | |
| cleaned.append(gray) | |
| margin, gap = 24, 20 | |
| canvas = Image.new("L", (max(im.width for im in cleaned) + 2 * margin, | |
| sum(im.height for im in cleaned) + gap * (len(cleaned)-1) + 2 * margin), 255) | |
| y = margin | |
| for image in cleaned: | |
| canvas.paste(image, (margin, y)) | |
| y += image.height + gap | |
| return canvas | |