Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import concurrent.futures
|
|
| 8 |
import threading
|
| 9 |
import base64
|
| 10 |
import io
|
|
|
|
| 11 |
from itertools import chain
|
| 12 |
from PIL import Image
|
| 13 |
from datetime import datetime
|
|
@@ -114,14 +115,32 @@ def test_model_availability(api_key, model_name):
|
|
| 114 |
)
|
| 115 |
return False
|
| 116 |
|
| 117 |
-
def create_base64_markdown_image(image_url):
|
| 118 |
try:
|
| 119 |
response = requests.get(image_url, stream=True)
|
| 120 |
response.raise_for_status()
|
| 121 |
|
| 122 |
image_data = response.content
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
markdown_image_link = f""
|
| 126 |
logging.info(f"Created base64 markdown image link.")
|
| 127 |
return markdown_image_link
|
|
|
|
| 8 |
import threading
|
| 9 |
import base64
|
| 10 |
import io
|
| 11 |
+
from io import BytesIO
|
| 12 |
from itertools import chain
|
| 13 |
from PIL import Image
|
| 14 |
from datetime import datetime
|
|
|
|
| 115 |
)
|
| 116 |
return False
|
| 117 |
|
| 118 |
+
def create_base64_markdown_image(image_url, max_width=None, max_height=None):
|
| 119 |
try:
|
| 120 |
response = requests.get(image_url, stream=True)
|
| 121 |
response.raise_for_status()
|
| 122 |
|
| 123 |
image_data = response.content
|
| 124 |
+
image = Image.open(BytesIO(image_data))
|
| 125 |
+
|
| 126 |
+
if max_width or max_height:
|
| 127 |
+
if max_width and max_height:
|
| 128 |
+
image.thumbnail((max_width, max_height), Image.LANCZOS)
|
| 129 |
+
elif max_width:
|
| 130 |
+
wpercent = (max_width / float(image.size[0]))
|
| 131 |
+
hsize = int((float(image.size[1]) * float(wpercent)))
|
| 132 |
+
image = image.resize((max_width, hsize), Image.LANCZOS)
|
| 133 |
+
elif max_height:
|
| 134 |
+
hpercent = (max_height / float(image.size[1]))
|
| 135 |
+
wsize = int((float(image.size[0]) * float(hpercent)))
|
| 136 |
+
image = image.resize((wsize, max_height), Image.LANCZOS)
|
| 137 |
+
|
| 138 |
+
buffered = BytesIO()
|
| 139 |
+
image.save(buffered, format="PNG")
|
| 140 |
+
compressed_image_data = buffered.getvalue()
|
| 141 |
+
|
| 142 |
+
base64_encoded = base64.b64encode(compressed_image_data).decode('utf-8')
|
| 143 |
+
mime_type = "image/png"
|
| 144 |
markdown_image_link = f""
|
| 145 |
logging.info(f"Created base64 markdown image link.")
|
| 146 |
return markdown_image_link
|