aifeifei798 commited on
Commit
ddf7ea9
·
verified ·
1 Parent(s): a70f43f

Update feifeilib/feifeiflorence.py

Browse files
Files changed (1) hide show
  1. feifeilib/feifeiflorence.py +18 -3
feifeilib/feifeiflorence.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import base64
2
  import requests
3
  import os
@@ -10,12 +12,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
 
 
1
+ from PIL import Image
2
+ from io import BytesIO
3
  import base64
4
  import requests
5
  import os
 
12
  def encode_image(image_path):
13
  """Encode the image to base64."""
14
  try:
15
+ # Open the image file
16
+ image = Image.open(image_path).convert("RGB")
17
+
18
+ # Resize the image to a height of 512 while maintaining the aspect ratio
19
+ base_height = 512
20
+ h_percent = (base_height / float(image.size[1]))
21
+ w_size = int((float(image.size[0]) * float(h_percent)))
22
+ image = image.resize((w_size, base_height), Image.LANCZOS)
23
+
24
+ # Convert the image to a byte stream
25
+ buffered = BytesIO()
26
+ image.save(buffered, format="JPEG")
27
+ img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
28
+
29
+ return img_str
30
  except FileNotFoundError:
31
  print(f"Error: The file {image_path} was not found.")
32
  return None
33
+ except Exception as e: # Add generic exception handling
34
  print(f"Error: {e}")
35
  return None
36