Upload app.py
Browse files683 unmodified lines684685686687688689690691692693694695696697698699700701702703704705706707708709710201 unmodified lines9129139149159169179189199209219229239242 unmodified lines927928929930931932933683 unmodified linesreturn 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 andplace the entire original image, scaled to fit, in the center. This avoids hard bars andpreserves the original framing instead of center-cropping the person."""from PIL import Image as _Image, ImageOps as _ImageOps, ImageFilter as _ImageFilterh, 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:return image_pathbackground = _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) // 2y = (h - foreground.height) // 2background.paste(foreground, (x, y))background.save(image_path)return image_pathdef _fit_keyframe(image_path, current_canvas):201 unmodified linescorrected = 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?","-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 linesresized = Trueprint(f"[canvas] Model/encoder output {actual_width}x{actual_height}; "f"final MP4 fitted to selected Canvas without cropping {width}x{height}.",flush=True,)683 unmodified lines684685686687688689690691692693694695696697698699700701201 unmodified lines9039049059069079089099109119122 unmodified lines915916917918919920921683 unmodified linesreturn fastest[ratio][0]def _cover_crop(image_path: str, canvas_label: str) -> str:"""Resize/crop the keyframe to fill the selected canvas, with no blurred background."""from PIL import Image as _Image, ImageOps as _ImageOpsh, w = CANVASES[canvas_label]with _Image.open(image_path) as source:img = _ImageOps.exif_transpose(source).convert("RGB")if img.size == (w, h):return 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_pathdef _fit_keyframe(image_path, current_canvas):201 unmodified linescorrected = path[:-4] + "-canvas.mp4"subprocess.run([ffmpeg, "-y", "-i", path,"-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 linesresized = Trueprint(f"[canvas] Model/encoder output {actual_width}x{actual_height}; "f"final MP4 filled to selected Canvas (excess edges cropped) {width}x{height}.",flush=True,)
app.py+12-24