Resize Right
Resize Right is a Python library providing a single, highly flexible `resize` function for image resizing. It supports both NumPy arrays and PyTorch tensors, offering various scaling options, padding modes, and output shape controls. The current version is 0.0.2. Releases appear to be infrequent, indicating a stable, focused utility rather than a rapidly evolving project.
Common errors
-
AttributeError: module 'resize_right' has no attribute 'resize'
cause The `resize` function is directly imported from the `resize_right` module, not accessed as an attribute of the module object.fixEnsure the import statement is `from resize_right import resize`. Then call it directly as `resize(...)` instead of `resize_right.resize(...)`. -
ValueError: input_array should be a torch tensor or numpy array, but got <class 'PIL.Image.Image'>
cause The `resize` function only accepts NumPy arrays or PyTorch tensors as input, not native image objects from libraries like Pillow (PIL).fixConvert PIL Image objects to NumPy arrays first using `np.array(pil_image)` or PyTorch tensors using `torch.from_numpy(np.array(pil_image))` before passing them to the `resize` function. -
TypeError: 'numpy.ndarray' object cannot be interpreted as an integer
cause This often occurs when `out_shape` or `scale_factors` parameters are not provided in the expected format (e.g., an array where a scalar or list of integers is expected).fixEnsure `out_shape` is a list or tuple of two integers (e.g., `[50, 50]`) or `scale_factors` is a single float or an iterable of two floats (e.g., `0.5` or `[0.5, 0.5]`) for specifying the resizing dimensions.
Warnings
- gotcha Input array layout for NumPy: `resize_right` expects NumPy arrays in `(batch, height, width, channels)` format. Providing `(batch, channels, height, width)` (common in PyTorch) will lead to incorrect resizing or errors due to misinterpretation of dimensions.
- gotcha Conflicting `out_shape` and `scale_factors` parameters: If both `out_shape` and `scale_factors` are provided, `out_shape` will take precedence, and `scale_factors` will be ignored. This might lead to unexpected output dimensions if the user intends `scale_factors` to be the primary control.
- gotcha Handling of non-float data types: While `resize_right` generally works with various numeric types, it's primarily designed for floating-point image data (e.g., `float32`, `float64`) common in deep learning pipelines. Using integer types might lead to precision loss or unexpected results during interpolation, especially for downsampling.
Install
-
pip install resize-right
Imports
- resize
from resize_right import resize
Quickstart
import numpy as np
from resize_right import resize
# Create a dummy 4D NumPy array (batch, H, W, channels)
# representing a batch of 2 images, 100x100 pixels, 3 channels
image_batch = np.random.rand(2, 100, 100, 3).astype(np.float32)
print(f"Original shape: {image_batch.shape}")
# Resize to half the size using scale_factors
resized_batch_scale = resize(image_batch, scale_factors=0.5)
print(f"Resized by scale_factors (0.5) shape: {resized_batch_scale.shape}")
# Resize to a specific output shape [50, 50] (for H, W)
resized_batch_shape = resize(image_batch, out_shape=[50, 50])
print(f"Resized by out_shape ([50, 50]) shape: {resized_batch_shape.shape}")