Spaces:
Sleeping
Sleeping
Update src/helpers.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243import numpy as npfrom PIL import Imagedef resize_image(image_array, max_size=1048576):"""Resizes a NumPy image array to a maximum total size of 1048576 pixels while preserving the aspect ratio.Args:image_array (numpy.ndarray): The input image array.max_size (int): The maximum total size of the image in pixels (default is 1048576).Returns:numpy.ndarray: The resized image array."""# Get the current size of the imageheight, width, _ = image_array.shape# Calculate the total size of the imagetotal_size = height * width# If the image is already smaller than the max size, return the original imageif total_size <= max_size:return image_array# Calculate the aspect ratioaspect_ratio = width / height# Calculate the new dimensions while preserving the aspect ratioif aspect_ratio > 1:new_width = int(np.sqrt(max_size * aspect_ratio))new_height = int(new_width / aspect_ratio)else:new_height = int(np.sqrt(max_size / aspect_ratio))new_width = int(new_height * aspect_ratio)# Resize the image using Pillowimage = Image.fromarray(image_array)image = image.resize((new_width, new_height), resample=Image.BICUBIC)# Convert the resized image back to a NumPy arrayresized_image = np.array(image)return resized_image
src/helpers.py+43