in src/image_gen_aux/image_processor.py [0:0]
def resize_numpy_image(image: np.ndarray, scale: float, multiple_factor: int = 8) -> np.ndarray:
"""
Resizes a NumPy image while maintaining aspect ratio and ensuring dimensions are multiples of `multiple_factor`.
Args:
image (`np.ndarray`): The input image array of shape (height, width, channels) or (height, width) for grayscale.
scale (`float`): The scaling factor applied to the image dimensions.
multiple_factor (`int`, *optional*, defaults to 8): The factor by which the new dimensions should be divisible.
Returns:
`np.ndarray`: The resized image array.
"""
if scale == 1.0:
return image, scale
if len(image.shape) == 3: # Single image without batch dimension
image = np.expand_dims(image, axis=0)
batch_size, height, width, channels = image.shape
# Calculate new dimensions while maintaining aspect ratio
new_height = int(height * scale)
new_width = int(width * scale)
# Ensure new dimensions are multiples of multiple_factor
new_height = (new_height // multiple_factor) * multiple_factor
new_width = (new_width // multiple_factor) * multiple_factor
# if the final height and widht changed because of the multiple_factor, we need to set the scale too
scale = new_height / height
# Resize each image in the batch
resized_images = []
for i in range(batch_size):
resized_image = cv2.resize(image[i], (new_width, new_height), interpolation=cv2.INTER_LINEAR)
resized_images.append(resized_image)
# Stack resized images back into a single array
resized_images = np.stack(resized_images, axis=0)
return resized_images, scale