Upload app.py
Browse files683 unmodified lines684685686687688689690691692693687688689690691697698692693694695701702703704705706707696697698699700701201 unmodified lines9039049059159169179189199209219069079089099109119122 unmodified lines915916917930918919920921683 unmodified lines return fastest[ratio][0]
def _cover_crop(image_path: str, canvas_label: str) -> str: """Fit the complete keyframe to the selected canvas without cutting off the subject.
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, ImageOps as _ImageOps, ImageFilter as _ImageFilter """Resize/crop the keyframe to fill the selected canvas, with no blurred background.""" from PIL import Image as _Image, ImageOps as _ImageOps
h, w = CANVASES[canvas_label] with _Image.open(image_path) as source: img = source.convert("RGB") if abs(img.width / img.height - (w / h)) <= 1e-3: img = _ImageOps.exif_transpose(source).convert("RGB") if img.size == (w, h): return 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) # Fill the complete canvas; if aspect ratios differ, crop excess edges. fitted = _ImageOps.fit(img, (w, h), method=_Image.Resampling.LANCZOS) fitted.save(image_path) return image_path
def _fit_keyframe(image_path, current_canvas):201 unmodified lines corrected = path[:-4] + "-canvas.mp4" subprocess.run( [ffmpeg, "-y", "-i", path, "-filter_complex", f"[0:v]split=2[bg][fg];" 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?", "-vf", f"scale={width}:{height}:force_original_aspect_ratio=increase:flags=lanczos," f"crop={width}:{height},setsar=1", "-map", "0:v:0", "-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 fitted to selected Canvas without cropping {width}x{height}.", f"final MP4 filled to selected Canvas (excess edges cropped) {width}x{height}.", flush=True, )
app.py+12-24