Spaces:
Paused
Paused
Download engine/pipeline.py from Ribakoo/Image-Enhancer: direct link, hf CLI and curl.
- Browser
- Download file 3.65 kB
-
https://huggingface.co/spaces/Ribakoo/Image-Enhancer/resolve/main/engine/pipeline.py
- Command line
-
hf download hf://spaces/Ribakoo/Image-Enhancer/engine/pipeline.py
-
curl -L -o pipeline.py https://huggingface.co/spaces/Ribakoo/Image-Enhancer/resolve/main/engine/pipeline.py
3.65 kB
| """ | |
| Unified Processing Pipeline for VectorForge. | |
| Coordinates AI super-resolution and mathematical vectorization. | |
| """ | |
| from dataclasses import dataclass | |
| from pathlib import Path | |
| import time | |
| import uuid | |
| from typing import Optional, Dict, Any | |
| from PIL import Image | |
| from engine.upscaler import AI2DUpscaler | |
| from engine.vectorizer import Vectorizer | |
| class ProcessingResult: | |
| job_id: str | |
| mode: str | |
| original_size: tuple[int, int] | |
| enhanced_size: tuple[int, int] | |
| original_filesize: int | |
| enhanced_png_filesize: Optional[int] | |
| svg_filesize: Optional[int] | |
| elapsed_seconds: float | |
| png_url: Optional[str] | |
| svg_url: Optional[str] | |
| svg_preview: Optional[str] | |
| class ProcessingPipeline: | |
| def __init__(self, output_dir: Path): | |
| self.output_dir = Path(output_dir) | |
| self.output_dir.mkdir(parents=True, exist_ok=True) | |
| self.upscaler = AI2DUpscaler() | |
| def process( | |
| self, | |
| input_image_path: Path, | |
| mode: str = "hybrid", # "hybrid", "ai_upscale", "vectorize" | |
| scale: int = 4, # 2 or 4 | |
| denoise: bool = True, | |
| vector_preset: str = "character", | |
| custom_vector_params: Optional[Dict[str, Any]] = None, | |
| ) -> ProcessingResult: | |
| t0 = time.time() | |
| job_id = uuid.uuid4().hex[:12] | |
| with Image.open(input_image_path) as img: | |
| orig_w, orig_h = img.size | |
| orig_filesize = input_image_path.stat().st_size | |
| png_path: Optional[Path] = None | |
| svg_path: Optional[Path] = None | |
| enhanced_w, enhanced_h = orig_w, orig_h | |
| svg_preview_content: Optional[str] = None | |
| if mode in ("hybrid", "ai_upscale"): | |
| png_path = self.output_dir / f"{job_id}_enhanced.png" | |
| self.upscaler.upscale_file( | |
| input_image_path, | |
| png_path, | |
| scale=scale, | |
| denoise=denoise, | |
| ) | |
| with Image.open(png_path) as up_img: | |
| enhanced_w, enhanced_h = up_img.size | |
| if mode == "vectorize": | |
| svg_path = self.output_dir / f"{job_id}_vector.svg" | |
| Vectorizer.vectorize_file( | |
| input_image_path, | |
| svg_path, | |
| preset=vector_preset, | |
| custom_params=custom_vector_params, | |
| ) | |
| enhanced_w, enhanced_h = orig_w, orig_h | |
| elif mode == "hybrid": | |
| # Vectorize the cleaned/upscaled raster | |
| svg_path = self.output_dir / f"{job_id}_vector.svg" | |
| Vectorizer.vectorize_file( | |
| png_path, | |
| svg_path, | |
| preset=vector_preset, | |
| custom_params=custom_vector_params, | |
| ) | |
| # Read SVG content for inline UI preview (up to 200KB) | |
| if svg_path and svg_path.exists(): | |
| svg_text = svg_path.read_text(encoding="utf-8") | |
| svg_preview_content = svg_text | |
| elapsed = round(time.time() - t0, 2) | |
| return ProcessingResult( | |
| job_id=job_id, | |
| mode=mode, | |
| original_size=(orig_w, orig_h), | |
| enhanced_size=(enhanced_w, enhanced_h), | |
| original_filesize=orig_filesize, | |
| enhanced_png_filesize=png_path.stat().st_size if png_path and png_path.exists() else None, | |
| svg_filesize=svg_path.stat().st_size if svg_path and svg_path.exists() else None, | |
| elapsed_seconds=elapsed, | |
| png_url=f"/api/download/{job_id}/png" if png_path else None, | |
| svg_url=f"/api/download/{job_id}/svg" if svg_path else None, | |
| svg_preview=svg_preview_content, | |
| ) | |