Spaces:
Sleeping
Sleeping
Update my_model/state_manager.py
Browse files- my_model/state_manager.py +29 -14
my_model/state_manager.py
CHANGED
|
@@ -256,33 +256,48 @@ class StateManager:
|
|
| 256 |
"""
|
| 257 |
return st.session_state['images_data']
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
|
|
|
| 261 |
"""
|
| 262 |
-
Resize an image
|
| 263 |
|
| 264 |
Args:
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
|
| 269 |
Returns:
|
| 270 |
-
Image.Image: The resized
|
| 271 |
"""
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
# Open the image from a file path
|
| 276 |
-
image = Image.open(
|
| 277 |
-
elif isinstance(
|
| 278 |
-
self.col2.write("Image")
|
| 279 |
# Use the image directly if it's already a PIL Image object
|
| 280 |
image = image_input
|
| 281 |
else:
|
| 282 |
raise ValueError("image_input must be a file path or a PIL Image object")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
-
resized_image = image.resize((new_width, new_height))
|
| 285 |
return resized_image
|
|
|
|
| 286 |
|
| 287 |
|
| 288 |
def update_image_data(self, image_key, caption, detected_objects_str, analysis_done):
|
|
|
|
| 256 |
"""
|
| 257 |
return st.session_state['images_data']
|
| 258 |
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
def resize_image_aspect_ratio(self,image_input, base_width=None, base_height=None):
|
| 262 |
"""
|
| 263 |
+
Resize an image while maintaining its aspect ratio.
|
| 264 |
|
| 265 |
Args:
|
| 266 |
+
image (PIL.Image.Image): The image to resize.
|
| 267 |
+
base_width (int, optional): The target width, with the height adjusted to maintain aspect ratio.
|
| 268 |
+
base_height (int, optional): The target height, with the width adjusted to maintain aspect ratio.
|
| 269 |
|
| 270 |
Returns:
|
| 271 |
+
PIL.Image.Image: The resized image.
|
| 272 |
"""
|
| 273 |
+
|
| 274 |
+
image = copy.deepcopy(image_input)
|
| 275 |
+
if isinstance(image, str):
|
| 276 |
# Open the image from a file path
|
| 277 |
+
image = Image.open(image)
|
| 278 |
+
elif isinstance(image, Image.Image):
|
|
|
|
| 279 |
# Use the image directly if it's already a PIL Image object
|
| 280 |
image = image_input
|
| 281 |
else:
|
| 282 |
raise ValueError("image_input must be a file path or a PIL Image object")
|
| 283 |
+
|
| 284 |
+
# Get original dimensions
|
| 285 |
+
original_width, original_height = image.size
|
| 286 |
+
|
| 287 |
+
# Calculate new dimensions
|
| 288 |
+
if base_width is not None:
|
| 289 |
+
ratio = (base_width / float(original_width))
|
| 290 |
+
new_height = int((float(original_height) * float(ratio)))
|
| 291 |
+
resized_image = image.resize((base_width, new_height))
|
| 292 |
+
elif base_height is not None:
|
| 293 |
+
ratio = (base_height / float(original_height))
|
| 294 |
+
new_width = int((float(original_width) * float(ratio)))
|
| 295 |
+
resized_image = image.resize((new_width, base_height))
|
| 296 |
+
else:
|
| 297 |
+
raise ValueError("Either base_width or base_height must be provided")
|
| 298 |
|
|
|
|
| 299 |
return resized_image
|
| 300 |
+
|
| 301 |
|
| 302 |
|
| 303 |
def update_image_data(self, image_key, caption, detected_objects_str, analysis_done):
|