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

Fix weird resized width and height if the difference of image ratio is large #18

Merged
merged 4 commits into from
Apr 10, 2024
Merged
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: 8 additions & 3 deletions wpodnet/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,17 @@ def _resize_to_fixed_ratio(self, image: Image.Image, dim_min: int, dim_max: int)
side = int(wh_ratio * dim_min)
bound_dim = min(side + side % self._stride, dim_max)

factor = bound_dim / min(h, w)
factor = bound_dim / max(h, w)
reg_w, reg_h = int(w * factor), int(h * factor)

# Ensure the both width and height are the multiply of `self._stride`
reg_w += self._stride - reg_w % self._stride
reg_h += self._stride - reg_h % self._stride
reg_w_mod = reg_w % self._stride
if reg_w_mod > 0:
reg_w += self._stride - reg_w_mod

reg_h_mod = reg_h % self._stride
if reg_h_mod > 0:
reg_h += self._stride - reg_h % self._stride

return image.resize((reg_w, reg_h))

Expand Down
Loading