Skip to content
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

[DRAFT] feat: allow fixed resize #427

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ This module exposes a single function `download` which takes the same arguments
* **keep_ratio** will keep the ratio and make the smallest side of the picture image_size
* **keep_ratio_largest** will keep the ratio and make the largest side of the picture image_size
* **center_crop** will keep the ratio and center crop the largest side so the picture is squared
* **fixed** will resize both sides to image_size so the picture is squared
* **resize_only_if_bigger** resize pictures only if bigger that the image_size (default *False*)
* **upscale_interpolation** kind of upscale interpolation used for resizing (default *"lanczos"*)
* **downscale_interpolation** kind of downscale interpolation used for resizing (default *"area"*)
Expand Down
10 changes: 7 additions & 3 deletions img2dataset/resizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ResizeMode(Enum):
center_crop = 2 # pylint: disable=invalid-name
border = 3 # pylint: disable=invalid-name
keep_ratio_largest = 4 # pylint: disable=invalid-name
fixed = 5 # pylint: disable=invalid-name


# thanks https://stackoverflow.com/questions/11130156/suppress-stdout-stderr-print-from-python-functions
Expand Down Expand Up @@ -74,7 +75,7 @@ class Resizer:
Should be used to resize one image at a time

Options:
resize_mode: "no", "keep_ratio", "center_crop", "border"
resize_mode: "no", "keep_ratio", "center_crop", "border", "fixed"
resize_only_if_bigger: if True, resize only if image is bigger than image_size
image_size: size of the output image to resize
"""
Expand Down Expand Up @@ -186,11 +187,14 @@ def __call__(self, img_stream, blurring_bbox_list=None):
img = A.center_crop(img, self.image_size, self.image_size)
encode_needed = True
maybe_blur_still_needed = False
elif self.resize_mode in (ResizeMode.border, ResizeMode.keep_ratio_largest):
elif self.resize_mode in (ResizeMode.border, ResizeMode.keep_ratio_largest, ResizeMode.fixed):
downscale = max(original_width, original_height) > self.image_size
if not self.resize_only_if_bigger or downscale:
interpolation = self.downscale_interpolation if downscale else self.upscale_interpolation
img = A.longest_max_size(img, self.image_size, interpolation=interpolation)
if self.resize_mode == ResizeMode.fixed:
img = A.resize(img, self.image_size, self.image_size, interpolation=interpolation)
else:
img = A.longest_max_size(img, self.image_size, interpolation=interpolation)
if blurring_bbox_list is not None and self.blurrer is not None:
img = self.blurrer(img=img, bbox_list=blurring_bbox_list)
if self.resize_mode == ResizeMode.border:
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def check_one_image_size(img, img_unresized, image_size, resize_mode, resize_onl
if resize_only_if_bigger:
if (
max(width_unresized, height_unresized) <= image_size
and resize_mode == "border"
and resize_mode in ["border", "fixed"]
or min(width_unresized, height_unresized) <= image_size
and resize_mode in ["keep_ratio", "center_crop"]
):
Expand All @@ -137,7 +137,7 @@ def check_one_image_size(img, img_unresized, image_size, resize_mode, resize_onl
if not resized:
return

if resize_mode == "border":
if resize_mode in ["border", "fixed"]:
if width != image_size or height != image_size:
raise Exception(f"Image size is not 256x256 in border mode found={width}x{height}")
elif resize_mode == "keep_ratio":
Expand Down
Loading