Skip to content

[fix] Flux Kontext resize method #11863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/diffusers/image_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,17 +467,17 @@ def _resize_and_crop(

def resize(
self,
image: Union[PIL.Image.Image, np.ndarray, torch.Tensor],
image: PipelineImageInput,
height: int,
width: int,
resize_mode: str = "default", # "default", "fill", "crop"
) -> Union[PIL.Image.Image, np.ndarray, torch.Tensor]:
) -> PipelineImageInput:
"""
Resize image.
Args:
image (`PIL.Image.Image`, `np.ndarray` or `torch.Tensor`):
The image input, can be a PIL image, numpy array or pytorch tensor.
image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `List[torch.Tensor]`, `List[PIL.Image.Image]`, or `List[np.ndarray]`):
The image batch input, can be a PIL image, numpy array or pytorch tensor, or a list of these elements.
height (`int`):
The height to resize to.
width (`int`):
Expand All @@ -492,7 +492,7 @@ def resize(
supported for PIL image input.
Returns:
`PIL.Image.Image`, `np.ndarray` or `torch.Tensor`:
`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `List[torch.Tensor]`, `List[PIL.Image.Image]`, or `List[np.ndarray]`:
The resized image.
"""
if resize_mode != "default" and not isinstance(image, PIL.Image.Image):
Expand Down Expand Up @@ -523,6 +523,16 @@ def resize(
size=(height, width),
)
image = self.pt_to_numpy(image)
elif isinstance(image, list):
image = [
self.resize(
img,
height=height,
width=width,
resize_mode=resize_mode,
)
for img in image
]
return image

def binarize(self, image: PIL.Image.Image) -> PIL.Image.Image:
Expand Down
13 changes: 13 additions & 0 deletions tests/others/test_image_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,16 @@ def test_vae_image_processor_resize_np(self):
assert out_np.shape == exp_np_shape, (
f"resized image output shape '{out_np.shape}' didn't match expected shape '{exp_np_shape}'."
)

def test_vae_image_processor_resize_list_pt(self):
image_processor = VaeImageProcessor(do_resize=True, vae_scale_factor=1)
input_pt = self.dummy_sample
b, c, h, w = input_pt.shape
scale = 2
input_pt_list = [input_pt]
out_pt_list = image_processor.resize(image=input_pt_list, height=h // scale, width=w // scale)
exp_pt_shape = (b, c, h // scale, w // scale)
for out_pt in out_pt_list:
assert out_pt.shape == exp_pt_shape, (
f"resized image output shape '{out_pt.shape}' didn't match expected shape '{exp_pt_shape}'."
)