Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
except FileNotFoundError:
|
| 16 |
print(f"Error: The file {image_path} was not found.")
|
| 17 |
return None
|
| 18 |
-
except Exception as e: #
|
| 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 |
|