Spaces:
Sleeping
Sleeping
Download src/dot2dot/rendering.py from DataWisp/dot2dot-generator-kids: direct link, hf CLI and curl.
- Browser
- Download file 3.44 kB
-
https://huggingface.co/spaces/DataWisp/dot2dot-generator-kids/resolve/main/src/dot2dot/rendering.py
- Command line
-
hf download hf://spaces/DataWisp/dot2dot-generator-kids/src/dot2dot/rendering.py
-
curl -L -o rendering.py https://huggingface.co/spaces/DataWisp/dot2dot-generator-kids/resolve/main/src/dot2dot/rendering.py
3.44 kB
| from __future__ import annotations | |
| from pathlib import Path | |
| from PIL import Image, ImageDraw, ImageFont | |
| from .config import AgePreset | |
| from .generator import DotLayout | |
| def _load_font(size: int) -> ImageFont.ImageFont: | |
| try: | |
| return ImageFont.truetype("DejaVuSans.ttf", size=size) | |
| except OSError: | |
| return ImageFont.load_default() | |
| def _fit_canvas(source: Image.Image, target_size, padding=220): | |
| source = source.convert("L") | |
| canvas = Image.new("RGB", target_size, "white") | |
| inner_width = target_size[0] - (padding * 2) | |
| inner_height = target_size[1] - (padding * 2) | |
| fitted = source.copy() | |
| fitted.thumbnail((inner_width, inner_height)) | |
| x = (target_size[0] - fitted.width) // 2 | |
| y = (target_size[1] - fitted.height) // 2 | |
| return canvas, fitted, (x, y) | |
| def render_activity_page( | |
| source: Image.Image, | |
| layout: DotLayout, | |
| preset: AgePreset, | |
| title: str, | |
| output_path: Path, | |
| include_reference: bool, | |
| page_pixel_size: tuple[int, int], | |
| ) -> Path: | |
| canvas, fitted, origin = _fit_canvas(source, target_size=page_pixel_size) | |
| draw = ImageDraw.Draw(canvas) | |
| font = _load_font(preset.font_size) | |
| title_font = _load_font(42) | |
| if include_reference: | |
| faint = fitted.convert("RGB") | |
| faint = Image.eval(faint, lambda px: 245 if px > 180 else 210) | |
| canvas.paste(faint, origin) | |
| sx = fitted.width / layout.source_size[0] | |
| sy = fitted.height / layout.source_size[1] | |
| draw.text((160, 100), title, fill="black", font=title_font) | |
| draw.text((160, 150), "Ligue os pontos em ordem.", fill="black", font=font) | |
| for index, (x, y) in enumerate(layout.points, start=1): | |
| px = int(origin[0] + x * sx) | |
| py = int(origin[1] + y * sy) | |
| radius = preset.dot_radius | |
| draw.ellipse((px - radius, py - radius, px + radius, py + radius), fill="white", outline="black", width=2) | |
| tx = px + radius + 4 | |
| ty = py - preset.font_size | |
| draw.text((tx, ty), str(index), fill="black", font=font) | |
| canvas.save(output_path, dpi=(300, 300)) | |
| return output_path | |
| def render_answer_page( | |
| source: Image.Image, | |
| layout: DotLayout, | |
| preset: AgePreset, | |
| title: str, | |
| output_path: Path, | |
| page_pixel_size: tuple[int, int], | |
| ) -> Path: | |
| canvas, fitted, origin = _fit_canvas(source, target_size=page_pixel_size) | |
| draw = ImageDraw.Draw(canvas) | |
| font = _load_font(preset.font_size) | |
| title_font = _load_font(42) | |
| outline = fitted.convert("RGB") | |
| outline = Image.eval(outline, lambda px: 255 if px > 210 else 225) | |
| canvas.paste(outline, origin) | |
| sx = fitted.width / layout.source_size[0] | |
| sy = fitted.height / layout.source_size[1] | |
| scaled_points = [(int(origin[0] + x * sx), int(origin[1] + y * sy)) for x, y in layout.points] | |
| draw.text((160, 100), f"{title} - Resposta", fill="black", font=title_font) | |
| if len(scaled_points) > 1: | |
| line_points = scaled_points + [scaled_points[0]] if layout.closed else scaled_points | |
| draw.line(line_points, fill="black", width=4, joint="curve") | |
| for index, (px, py) in enumerate(scaled_points, start=1): | |
| radius = max(5, preset.dot_radius - 2) | |
| draw.ellipse((px - radius, py - radius, px + radius, py + radius), fill="black") | |
| draw.text((px + radius + 3, py - preset.font_size), str(index), fill="black", font=font) | |
| canvas.save(output_path, dpi=(300, 300)) | |
| return output_path | |