MCPcopy Index your code
hub / github.com/huggingface/diffusers / _resize_and_fill

Method _resize_and_fill

src/diffusers/image_processor.py:377–427  ·  view source on GitHub ↗

r""" Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, filling empty with data from image. Args: image (`PIL.Image.Image`): The image to resize and fill.

(
        self,
        image: PIL.Image.Image,
        width: int,
        height: int,
    )

Source from the content-addressed store, hash-verified

375 return x1, y1, x2, y2
376
377 def _resize_and_fill(
378 self,
379 image: PIL.Image.Image,
380 width: int,
381 height: int,
382 ) -> PIL.Image.Image:
383 r"""
384 Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center
385 the image within the dimensions, filling empty with data from image.
386
387 Args:
388 image (`PIL.Image.Image`):
389 The image to resize and fill.
390 width (`int`):
391 The width to resize the image to.
392 height (`int`):
393 The height to resize the image to.
394
395 Returns:
396 `PIL.Image.Image`:
397 The resized and filled image.
398 """
399
400 ratio = width / height
401 src_ratio = image.width / image.height
402
403 src_w = width if ratio < src_ratio else image.width * height // image.height
404 src_h = height if ratio >= src_ratio else image.height * width // image.width
405
406 resized = image.resize((src_w, src_h), resample=PIL_INTERPOLATION[self.config.resample])
407 res = Image.new("RGB", (width, height))
408 res.paste(resized, box=(width // 2 - src_w // 2, height // 2 - src_h // 2))
409
410 if ratio < src_ratio:
411 fill_height = height // 2 - src_h // 2
412 if fill_height > 0:
413 res.paste(resized.resize((width, fill_height), box=(0, 0, width, 0)), box=(0, 0))
414 res.paste(
415 resized.resize((width, fill_height), box=(0, resized.height, width, resized.height)),
416 box=(0, fill_height + src_h),
417 )
418 elif ratio > src_ratio:
419 fill_width = width // 2 - src_w // 2
420 if fill_width > 0:
421 res.paste(resized.resize((fill_width, height), box=(0, 0, 0, height)), box=(0, 0))
422 res.paste(
423 resized.resize((fill_width, height), box=(resized.width, 0, resized.width, height)),
424 box=(fill_width + src_w, 0),
425 )
426
427 return res
428
429 def _resize_and_crop(
430 self,

Callers 1

resizeMethod · 0.95

Calls 2

newMethod · 0.80
resizeMethod · 0.45

Tested by

no test coverage detected