aifeifei798 commited on
Commit
c0dcd29
·
verified ·
1 Parent(s): 73adad0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -3
app.py CHANGED
@@ -10,12 +10,25 @@ Mistralclient = Mistral(api_key=api_key)
10
  def encode_image(image_path):
11
  """Encode the image to base64."""
12
  try:
13
- with open(image_path, "rb") as image_file:
14
- return base64.b64encode(image_file.read()).decode('utf-8')
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  except FileNotFoundError:
16
  print(f"Error: The file {image_path} was not found.")
17
  return None
18
- except Exception as e: # Added general exception handling
19
  print(f"Error: {e}")
20
  return None
21
 
 
10
  def encode_image(image_path):
11
  """Encode the image to base64."""
12
  try:
13
+ # Open the image file
14
+ image = Image.open(image_path).convert("RGB")
15
+
16
+ # Resize the image to a height of 520 while maintaining the aspect ratio
17
+ base_height = 520
18
+ h_percent = (base_height / float(image.size[1]))
19
+ w_size = int((float(image.size[0]) * float(h_percent)))
20
+ image = image.resize((w_size, base_height), Image.ANTIALIAS)
21
+
22
+ # Convert the image to a byte stream
23
+ buffered = BytesIO()
24
+ image.save(buffered, format="JPEG")
25
+ img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
26
+
27
+ return img_str
28
  except FileNotFoundError:
29
  print(f"Error: The file {image_path} was not found.")
30
  return None
31
+ except Exception as e: # Add generic exception handling
32
  print(f"Error: {e}")
33
  return None
34