Update my_model/state_manager.py
Browse files- my_model/state_manager.py +35 -36
my_model/state_manager.py
CHANGED
|
@@ -256,47 +256,46 @@ class StateManager:
|
|
| 256 |
"""
|
| 257 |
return st.session_state['images_data']
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
if isinstance(image, str):
|
| 276 |
# Open the image from a file path
|
| 277 |
-
image = Image.open(
|
| 278 |
-
elif isinstance(
|
| 279 |
# Use the image directly if it's already a PIL Image object
|
| 280 |
-
image =
|
| 281 |
else:
|
| 282 |
raise ValueError("image_input must be a file path or a PIL Image object")
|
| 283 |
-
|
| 284 |
-
|
|
|
|
| 285 |
original_width, original_height = image.size
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
return resized_image
|
| 300 |
|
| 301 |
|
| 302 |
|
|
|
|
| 256 |
"""
|
| 257 |
return st.session_state['images_data']
|
| 258 |
|
| 259 |
+
def resize_image(image_input, new_width=None, new_height=None):
|
| 260 |
+
"""
|
| 261 |
+
Resize an image. If only new_width is provided, the height is adjusted to maintain aspect ratio.
|
| 262 |
+
If both new_width and new_height are provided, the image is resized to those dimensions.
|
| 263 |
+
|
| 264 |
+
Args:
|
| 265 |
+
image (PIL.Image.Image): The image to resize.
|
| 266 |
+
new_width (int, optional): The target width of the image.
|
| 267 |
+
new_height (int, optional): The target height of the image.
|
| 268 |
+
|
| 269 |
+
Returns:
|
| 270 |
+
PIL.Image.Image: The resized image.
|
| 271 |
+
"""
|
| 272 |
+
|
| 273 |
+
img = copy.deepcopy(image_input)
|
| 274 |
+
if isinstance(img, str):
|
|
|
|
| 275 |
# Open the image from a file path
|
| 276 |
+
image = Image.open(img)
|
| 277 |
+
elif isinstance(img, Image.Image):
|
| 278 |
# Use the image directly if it's already a PIL Image object
|
| 279 |
+
image = img
|
| 280 |
else:
|
| 281 |
raise ValueError("image_input must be a file path or a PIL Image object")
|
| 282 |
+
|
| 283 |
+
if new_width is not None and new_height is None:
|
| 284 |
+
# Calculate new height to maintain aspect ratio
|
| 285 |
original_width, original_height = image.size
|
| 286 |
+
ratio = new_width / original_width
|
| 287 |
+
new_height = int(original_height * ratio)
|
| 288 |
+
elif new_width is None and new_height is not None:
|
| 289 |
+
# Calculate new width to maintain aspect ratio
|
| 290 |
+
original_width, original_height = image.size
|
| 291 |
+
ratio = new_height / original_height
|
| 292 |
+
new_width = int(original_width * ratio)
|
| 293 |
+
elif new_width is None and new_height is None:
|
| 294 |
+
raise ValueError("At least one of new_width or new_height must be provided")
|
| 295 |
+
|
| 296 |
+
# Resize the image
|
| 297 |
+
resized_image = image.resize((new_width, new_height))
|
| 298 |
+
return resized_image
|
|
|
|
| 299 |
|
| 300 |
|
| 301 |
|