Skip to content
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
11 changes: 9 additions & 2 deletions src/diffusers/image_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import math
import warnings
from typing import Any

import numpy as np
import PIL.Image
Expand Down Expand Up @@ -885,7 +886,7 @@ def preprocess(
height: int | None = None,
width: int | None = None,
padding_mask_crop: int | None = None,
) -> tuple[torch.Tensor, torch.Tensor]:
) -> tuple[torch.Tensor, torch.Tensor | None, dict[str, Any]]:
"""
Preprocess the image and mask.
"""
Expand All @@ -894,7 +895,13 @@ def preprocess(

# if mask is None, same behavior as regular image processor
if mask is None:
return self._image_processor.preprocess(image, height=height, width=width)
processed_image = self._image_processor.preprocess(image, height=height, width=width)
postprocessing_kwargs = {
"crops_coords": None,
"original_image": None,
"original_mask": None,
}
return processed_image, None, postprocessing_kwargs

if padding_mask_crop is not None:
crops_coords = self._image_processor.get_crop_region(mask, width, height, pad=padding_mask_crop)
Expand Down
38 changes: 37 additions & 1 deletion tests/others/test_image_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

import numpy as np
import PIL.Image
import pytest
import torch

from diffusers.image_processor import VaeImageProcessor
from diffusers.image_processor import InpaintProcessor, VaeImageProcessor


class TestImageProcessor:
Expand Down Expand Up @@ -306,3 +307,38 @@ 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}'."
)

@pytest.mark.parametrize(
"has_mask, padding_mask_crop",
[
(True, None),
(False, None),
(True, 8),
],
)
def test_inpaint_processor_preprocess(self, has_mask, padding_mask_crop):
processor = InpaintProcessor()
image = PIL.Image.fromarray(np.zeros((64, 64, 3), dtype=np.uint8))
mask = PIL.Image.fromarray(np.ones((64, 64), dtype=np.uint8) * 255) if has_mask else None

kwargs = {"height": 64, "width": 64}
if padding_mask_crop is not None:
kwargs["padding_mask_crop"] = padding_mask_crop

out_img, out_mask, postprocessing_kwargs = processor.preprocess(image, mask=mask, **kwargs)

assert isinstance(out_img, torch.Tensor)
if has_mask:
assert isinstance(out_mask, torch.Tensor)
else:
assert out_mask is None
assert isinstance(postprocessing_kwargs, dict)

if padding_mask_crop is not None:
assert postprocessing_kwargs["crops_coords"] is not None
assert postprocessing_kwargs["original_image"] == image
assert postprocessing_kwargs["original_mask"] == mask
else:
assert postprocessing_kwargs["crops_coords"] is None
assert postprocessing_kwargs["original_image"] is None
assert postprocessing_kwargs["original_mask"] is None
Loading