Use PIL Lanczos resize in resize_keyframe (F.interpolate no-4D-lanczos)

This commit is contained in:
Daniel Maddern 2026-08-19 21:56:32 +07:00
parent 0b7217485c
commit 453aa86328

View file

@ -340,7 +340,16 @@ def resize_keyframe(image: torch.Tensor, width: int, height: int, *, crop: str =
elif old_aspect < new_aspect:
y = round((old_h - old_h * (old_aspect / new_aspect)) / 2)
samples = samples.narrow(-2, y, old_h - 2 * y).narrow(-1, x, old_w - 2 * x)
samples = F.interpolate(samples, size=(height, width), mode="lanczos")
# F.interpolate(4D, lanczos) is not supported by PyTorch; emulate via
# upsample-then-downsample with nearest + a small Lanczos-3 kernel.
# (Simpler: just use PIL's LANCZOS via numpy for the single image.)
import numpy as np
from PIL import Image as PILImage
arr = samples[0].permute(1, 2, 0).cpu().numpy() # [H, W, 3]
arr = (arr * 255.0).astype(np.uint8)
img = PILImage.fromarray(arr, mode="RGB")
img = img.resize((width, height), PILImage.LANCZOS)
samples = torch.from_numpy(np.asarray(img).astype(np.float32) / 255.0).permute(2, 0, 1).unsqueeze(0)
return samples.clamp(0.0, 1.0).movedim(1, -1) # [1, H, W, 3]