m7mdal7aj commited on
Commit
b287369
1 Parent(s): de93d42

Update my_model/state_manager.py

Browse files
Files changed (1) hide show
  1. 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
- 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
 
 
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