Upload app.py
Browse files683 unmodified lines684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714197 unmodified lines9129139149159169179189192 unmodified lines922923924925926927928683 unmodified linesreturn 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.The keyframe has to arrive at the model already matching the canvas, otherwise the pipelineletterboxes/stretches it and the first frame does not line up with the generated ones."""from PIL import Image as _Imageh, w = CANVASES[canvas_label]target = w / himg = _Image.open(image_path)if abs(img.width / img.height - target) <= 1e-3:return image_pathif img.width / img.height > target:new_w = int(img.height * target)left = (img.width - new_w) // 2img = img.crop((left, 0, left + new_w, img.height))else:new_h = int(img.width / target)top = (img.height - new_h) // 2img = img.crop((0, top, img.width, top + new_h))img.save(image_path)return image_pathdef _fit_keyframe(image_path, current_canvas):"""Crop a keyframe to the selected canvas ratio without changing the selected resolution."""return _cover_crop(image_path, current_canvas), current_canvasdef _fit_keyframe_ui(image_path, current_canvas):197 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}","-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 normalized to selected Canvas {width}x{height}.",flush=True,)683 unmodified lines684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714197 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):"""Fit a keyframe to the selected canvas ratio without changing the selected resolution."""return _cover_crop(image_path, current_canvas), current_canvasdef _fit_keyframe_ui(image_path, current_canvas):197 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,)
app.py+25-20