Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
import base64
|
3 |
import os
|
4 |
-
import requests
|
5 |
from PIL import Image
|
6 |
from io import BytesIO
|
7 |
|
@@ -9,15 +8,15 @@ from io import BytesIO
|
|
9 |
def compress_and_resize_image(image, max_size=(1024, 1024), quality=85):
|
10 |
img = Image.open(image)
|
11 |
img.thumbnail(max_size) # Resize image while maintaining aspect ratio
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
|
17 |
# Function to convert uploaded image to base64
|
18 |
def convert_image_to_base64(image):
|
19 |
compressed_image = compress_and_resize_image(image)
|
20 |
-
image_bytes = compressed_image.read()
|
21 |
encoded_image = base64.b64encode(image_bytes).decode("utf-8")
|
22 |
return encoded_image
|
23 |
|
|
|
1 |
import streamlit as st
|
2 |
import base64
|
3 |
import os
|
|
|
4 |
from PIL import Image
|
5 |
from io import BytesIO
|
6 |
|
|
|
8 |
def compress_and_resize_image(image, max_size=(1024, 1024), quality=85):
|
9 |
img = Image.open(image)
|
10 |
img.thumbnail(max_size) # Resize image while maintaining aspect ratio
|
11 |
+
byte_io = BytesIO()
|
12 |
+
img.save(byte_io, format="JPEG", quality=quality) # Save with reduced quality
|
13 |
+
byte_io.seek(0) # Make sure the pointer is at the beginning of the BytesIO buffer
|
14 |
+
return byte_io
|
15 |
|
16 |
# Function to convert uploaded image to base64
|
17 |
def convert_image_to_base64(image):
|
18 |
compressed_image = compress_and_resize_image(image)
|
19 |
+
image_bytes = compressed_image.read() # Read from BytesIO before closing
|
20 |
encoded_image = base64.b64encode(image_bytes).decode("utf-8")
|
21 |
return encoded_image
|
22 |
|