Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import os
|
|
4 |
import base64
|
5 |
from PIL import Image
|
6 |
from io import BytesIO
|
|
|
7 |
|
8 |
# Set page title and layout
|
9 |
st.set_page_config(page_title="Image Caption Generator", layout="centered")
|
@@ -24,14 +25,21 @@ def resize_image(image, max_width=500):
|
|
24 |
resized_image = image.resize((max_width, new_height), Image.Resampling.LANCZOS)
|
25 |
return resized_image
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# Function to convert image to Base64
|
28 |
-
def convert_to_base64(
|
29 |
"""
|
30 |
-
Convert the image to Base64 encoding.
|
31 |
"""
|
32 |
-
|
33 |
-
image.save(buffer, format="PNG")
|
34 |
-
image_base64 = base64.b64encode(buffer.getvalue()).decode()
|
35 |
return image_base64
|
36 |
|
37 |
# Function to call Nebius API
|
@@ -91,8 +99,11 @@ if uploaded_image:
|
|
91 |
# Display the resized image
|
92 |
st.image(resized_image, caption="Resized Image", use_container_width=True)
|
93 |
|
94 |
-
#
|
95 |
-
|
|
|
|
|
|
|
96 |
|
97 |
# Display Base64 output
|
98 |
st.subheader("Base64 Encoded String")
|
|
|
4 |
import base64
|
5 |
from PIL import Image
|
6 |
from io import BytesIO
|
7 |
+
import zlib # Compression library to reduce image size
|
8 |
|
9 |
# Set page title and layout
|
10 |
st.set_page_config(page_title="Image Caption Generator", layout="centered")
|
|
|
25 |
resized_image = image.resize((max_width, new_height), Image.Resampling.LANCZOS)
|
26 |
return resized_image
|
27 |
|
28 |
+
# Function to compress the image before converting to Base64
|
29 |
+
def compress_image(image, quality=70):
|
30 |
+
"""
|
31 |
+
Compress the image to reduce the size before Base64 encoding.
|
32 |
+
"""
|
33 |
+
buffered = BytesIO()
|
34 |
+
image.save(buffered, format="JPEG", quality=quality) # Save as JPEG with compression
|
35 |
+
return buffered.getvalue()
|
36 |
+
|
37 |
# Function to convert image to Base64
|
38 |
+
def convert_to_base64(image_data):
|
39 |
"""
|
40 |
+
Convert the compressed image to Base64 encoding.
|
41 |
"""
|
42 |
+
image_base64 = base64.b64encode(image_data).decode()
|
|
|
|
|
43 |
return image_base64
|
44 |
|
45 |
# Function to call Nebius API
|
|
|
99 |
# Display the resized image
|
100 |
st.image(resized_image, caption="Resized Image", use_container_width=True)
|
101 |
|
102 |
+
# Compress the image before converting to Base64
|
103 |
+
compressed_image_data = compress_image(resized_image)
|
104 |
+
|
105 |
+
# Convert compressed image to Base64
|
106 |
+
base64_string = convert_to_base64(compressed_image_data)
|
107 |
|
108 |
# Display Base64 output
|
109 |
st.subheader("Base64 Encoded String")
|