-
-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add Pillow resize backend #71
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
base: main
Are you sure you want to change the base?
feat: add Pillow resize backend #71
Conversation
Reviewer's GuideThis PR adds support for Pillow as an optional image resize backend by extending environment‐based backend detection and introducing a resize_pil implementation, streamlines the resize workflow with a unified early exit for images already at target dimensions while removing redundant checks, extends tests to cover Pillow vs OpenCV consistency with appropriate xfail and skip markers, and updates pyproject.toml to declare Pillow in optional dependencies. Entity relationship diagram for optional dependencies in pyproject.tomlerDiagram
PROJECT ||--o{ PILLOW : "optional dependency"
PROJECT ||--o{ PYVIPS : "optional dependency"
PROJECT ||--o{ PYTORCH : "optional dependency"
PROJECT {
string name
string version
string[] optional-dependencies
}
PILLOW {
string name
}
PYVIPS {
string name
}
PYTORCH {
string name
}
Class diagram for new and updated resize functionsclassDiagram
class functional {
+resize(img: np.ndarray, target_shape: tuple[int, int], interpolation: int) np.ndarray
+resize_cv2(img: np.ndarray, target_shape: tuple[int, int], interpolation: int) np.ndarray
+resize_pyvips(img: np.ndarray, target_shape: tuple[int, int], interpolation: int) np.ndarray
+resize_pil(img: np.ndarray, target_shape: tuple[int, int], interpolation: int) np.ndarray
}
functional --> "uses" PIL: resize_pil
functional --> "uses" cv2: resize_cv2
functional --> "uses" pyvips: resize_pyvips
functional --> "selects" resize_backend: resize
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
@federicopozzi33 Is it ready to be merged? |
I'll just add a test, and then it’s good to go. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- Consider adding the target_shape equality early-return inside the resize_pil backend (and similarly in other direct backend functions) to ensure callers bypass unnecessary work when dimensions already match.
- Instead of silently falling back to OpenCV when ALBUMENTATIONS_RESIZE is set to an unavailable backend, consider raising a configuration error or warning so users know their requested backend wasn't used.
- Pillow’s interpolation enum values don’t align with OpenCV’s indices, so adding an explicit mapping or validating supported interpolation values can help avoid unexpected resize artifacts when switching backends.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider adding the target_shape equality early-return inside the resize_pil backend (and similarly in other direct backend functions) to ensure callers bypass unnecessary work when dimensions already match.
- Instead of silently falling back to OpenCV when ALBUMENTATIONS_RESIZE is set to an unavailable backend, consider raising a configuration error or warning so users know their requested backend wasn't used.
- Pillow’s interpolation enum values don’t align with OpenCV’s indices, so adding an explicit mapping or validating supported interpolation values can help avoid unexpected resize artifacts when switching backends.
## Individual Comments
### Comment 1
<location> `albumentations/augmentations/geometric/functional.py:354` </location>
<code_context>
+def resize_pil(
</code_context>
<issue_to_address>
Validate input image format for PIL compatibility.
Ensure the input array matches PIL's expected format, or add validation and conversion steps as needed.
</issue_to_address>
### Comment 2
<location> `albumentations/augmentations/geometric/functional.py:301` </location>
<code_context>
-
height, width = img.shape[:2]
target_height, target_width = target_shape
original_dtype = img.dtype
</code_context>
<issue_to_address>
Restoring original dtype may cause issues for unsupported types.
Casting back to unsupported dtypes like int16 or float64 after resizing may corrupt data. Please restrict allowed dtypes or add a warning for unsupported types.
</issue_to_address>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
def resize_pil( | ||
img: np.ndarray, | ||
target_shape: tuple[int, int], | ||
interpolation: int, | ||
) -> np.ndarray: | ||
"""Resizes an image (NumPy array) using PIL's resize method. | ||
|
||
This function resizes an input image to the target shape using the specified interpolation method. | ||
|
||
Args: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Validate input image format for PIL compatibility.
Ensure the input array matches PIL's expected format, or add validation and conversion steps as needed.
@@ -290,9 +296,6 @@ | |||
# At this stage, the library's installation and importability have already been verified. | |||
import pyvips | |||
|
|||
if target_shape == img.shape[:2]: | |||
return img | |||
|
|||
height, width = img.shape[:2] | |||
target_height, target_width = target_shape | |||
original_dtype = img.dtype |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Restoring original dtype may cause issues for unsupported types.
Casting back to unsupported dtypes like int16 or float64 after resizing may corrupt data. Please restrict allowed dtypes or add a warning for unsupported types.
Summary by Sourcery
Add Pillow-based resize backend to geometric augmentations, centralize no-op shape checks, and extend dependency and test coverage accordingly
New Features:
Enhancements:
Build:
Tests: