Upload app.py
Browse files683 unmodified lines684685686687687688689690689690691692692693694695695696697696697698699699700701702703704705706707700701702703704705706707708709710711711712713714197 unmodified lines9129139149159169159169179189199209219229239242 unmodified lines927928929925930931932933683 unmodified lines return fastest[ratio][0]
def _cover_crop(image_path: str, canvas_label: str) -> str: """Center cover-crop the file at ``image_path`` to ``canvas_label``'s aspect ratio, in place. """Fit the complete keyframe to the selected canvas without cutting off the subject.
The keyframe has to arrive at the model already matching the canvas, otherwise the pipeline letterboxes/stretches it and the first frame does not line up with the generated ones. If aspect ratios differ, use a softly blurred, full-canvas version as the background and place the entire original image, scaled to fit, in the center. This avoids hard bars and preserves the original framing instead of center-cropping the person. """ from PIL import Image as _Image from PIL import Image as _Image, ImageOps as _ImageOps, ImageFilter as _ImageFilter
h, w = CANVASES[canvas_label] target = w / h img = _Image.open(image_path) if abs(img.width / img.height - target) <= 1e-3: with _Image.open(image_path) as source: img = source.convert("RGB") if abs(img.width / img.height - (w / h)) <= 1e-3: return image_path if img.width / img.height > target: new_w = int(img.height * target) left = (img.width - new_w) // 2 img = img.crop((left, 0, left + new_w, img.height)) else: new_h = int(img.width / target) top = (img.height - new_h) // 2 img = img.crop((0, top, img.width, top + new_h)) img.save(image_path)
background = _ImageOps.fit(img, (w, h), method=_Image.Resampling.LANCZOS) background = background.filter(_ImageFilter.GaussianBlur(radius=max(8, min(w, h) // 35))) foreground = _ImageOps.contain(img, (w, h), method=_Image.Resampling.LANCZOS) x = (w - foreground.width) // 2 y = (h - foreground.height) // 2 background.paste(foreground, (x, y)) background.save(image_path) return image_path
def _fit_keyframe(image_path, current_canvas): """Crop a keyframe to the selected canvas ratio without changing the selected resolution.""" """Fit a keyframe to the selected canvas ratio without changing the selected resolution.""" return _cover_crop(image_path, current_canvas), current_canvas
def _fit_keyframe_ui(image_path, current_canvas):197 unmodified lines corrected = path[:-4] + "-canvas.mp4" subprocess.run( [ffmpeg, "-y", "-i", path, "-filter_complex", f"[0:v]split=2[bg][fg];" "-vf", f"scale={width}:{height}:force_original_aspect_ratio=increase:flags=lanczos," f"crop={width}:{height}", f"[bg]scale={width}:{height}:force_original_aspect_ratio=increase:flags=lanczos," f"crop={width}:{height},boxblur=20:10[bg];" f"[fg]scale={width}:{height}:force_original_aspect_ratio=decrease:flags=lanczos[fg];" f"[bg][fg]overlay=(W-w)/2:(H-h)/2,setsar=1[v]", "-map", "[v]", "-map", "0:a?", "-c:v", "libx264", "-preset", "veryfast", "-crf", "18", "-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart", corrected], capture_output=True, text=True, check=True,2 unmodified lines resized = True print( f"[canvas] Model/encoder output {actual_width}x{actual_height}; " f"final MP4 normalized to selected Canvas {width}x{height}.", f"final MP4 fitted to selected Canvas without cropping {width}x{height}.", flush=True, )
app.py+25-20