Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,16 +14,16 @@ def compress_image(image, format='webp', quality=85):
|
|
14 |
img_byte_arr.seek(0)
|
15 |
return Image.open(img_byte_arr)
|
16 |
except Exception as e:
|
17 |
-
st.error(f"
|
18 |
st.error(traceback.format_exc())
|
19 |
return None
|
20 |
|
21 |
def main():
|
22 |
-
st.title("
|
23 |
|
24 |
-
st.sidebar.header("
|
25 |
|
26 |
-
uploaded_file = st.file_uploader("
|
27 |
|
28 |
if uploaded_file is not None:
|
29 |
try:
|
@@ -33,14 +33,14 @@ def main():
|
|
33 |
current_format = original_image.format.lower() if original_image.format else uploaded_file.name.split('.')[-1].lower()
|
34 |
current_format = 'jpeg' if current_format == 'jpg' else current_format
|
35 |
|
36 |
-
st.subheader("
|
37 |
-
st.image(original_image, caption=f"
|
38 |
|
39 |
original_size_bytes = len(uploaded_file.getvalue())
|
40 |
-
st.write(f"
|
41 |
|
42 |
compression_quality = st.sidebar.slider(
|
43 |
-
"
|
44 |
min_value=1,
|
45 |
max_value=100,
|
46 |
value=85
|
@@ -54,11 +54,11 @@ def main():
|
|
54 |
target_formats
|
55 |
)
|
56 |
|
57 |
-
st.warning("⚠️
|
58 |
|
59 |
progress_bar = st.progress(0)
|
60 |
|
61 |
-
if st.button("
|
62 |
try:
|
63 |
progress_bar.progress(20)
|
64 |
time.sleep(0.5)
|
@@ -86,16 +86,16 @@ def main():
|
|
86 |
optimized_size_bytes = img_byte_arr.getbuffer().nbytes
|
87 |
compression_ratio = (1 - optimized_size_bytes / original_size_bytes) * 100
|
88 |
|
89 |
-
st.subheader("
|
90 |
col1, col2 = st.columns(2)
|
91 |
|
92 |
with col1:
|
93 |
-
st.image(optimized_image, caption=f"
|
94 |
|
95 |
with col2:
|
96 |
-
st.write(f"
|
97 |
-
st.write(f"
|
98 |
-
st.write(f"
|
99 |
|
100 |
st.download_button(
|
101 |
label=f"Download {target_format.upper()}",
|
@@ -107,11 +107,11 @@ def main():
|
|
107 |
progress_bar.empty()
|
108 |
|
109 |
except Exception as e:
|
110 |
-
st.error(f"
|
111 |
progress_bar.empty()
|
112 |
|
113 |
except Exception as e:
|
114 |
-
st.error(f"
|
115 |
|
116 |
if __name__ == "__main__":
|
117 |
main()
|
|
|
14 |
img_byte_arr.seek(0)
|
15 |
return Image.open(img_byte_arr)
|
16 |
except Exception as e:
|
17 |
+
st.error(f"Image compression error: {e}")
|
18 |
st.error(traceback.format_exc())
|
19 |
return None
|
20 |
|
21 |
def main():
|
22 |
+
st.title("Image optimization tool")
|
23 |
|
24 |
+
st.sidebar.header("Optimization settings")
|
25 |
|
26 |
+
uploaded_file = st.file_uploader("Select an image", type=['jpg', 'png', 'jpeg', 'webp'])
|
27 |
|
28 |
if uploaded_file is not None:
|
29 |
try:
|
|
|
33 |
current_format = original_image.format.lower() if original_image.format else uploaded_file.name.split('.')[-1].lower()
|
34 |
current_format = 'jpeg' if current_format == 'jpg' else current_format
|
35 |
|
36 |
+
st.subheader("Original image")
|
37 |
+
st.image(original_image, caption=f"Original image ({current_format.upper()})")
|
38 |
|
39 |
original_size_bytes = len(uploaded_file.getvalue())
|
40 |
+
st.write(f"Original image size: {original_size_bytes} Bytes")
|
41 |
|
42 |
compression_quality = st.sidebar.slider(
|
43 |
+
"Compression quality",
|
44 |
min_value=1,
|
45 |
max_value=100,
|
46 |
value=85
|
|
|
54 |
target_formats
|
55 |
)
|
56 |
|
57 |
+
st.warning("⚠️ Please click 'Optimize Image' ONLY ONCE'!")
|
58 |
|
59 |
progress_bar = st.progress(0)
|
60 |
|
61 |
+
if st.button("Optimize image"):
|
62 |
try:
|
63 |
progress_bar.progress(20)
|
64 |
time.sleep(0.5)
|
|
|
86 |
optimized_size_bytes = img_byte_arr.getbuffer().nbytes
|
87 |
compression_ratio = (1 - optimized_size_bytes / original_size_bytes) * 100
|
88 |
|
89 |
+
st.subheader("Optimization results")
|
90 |
col1, col2 = st.columns(2)
|
91 |
|
92 |
with col1:
|
93 |
+
st.image(optimized_image, caption=f"Optimized ({target_format.upper()})")
|
94 |
|
95 |
with col2:
|
96 |
+
st.write(f"Original size: {original_size_bytes} Bytes")
|
97 |
+
st.write(f"Optimized size: {optimized_size_bytes} Bytes")
|
98 |
+
st.write(f"Compression rate: {compression_ratio:.2f}%")
|
99 |
|
100 |
st.download_button(
|
101 |
label=f"Download {target_format.upper()}",
|
|
|
107 |
progress_bar.empty()
|
108 |
|
109 |
except Exception as e:
|
110 |
+
st.error(f"Image optimization error: {e}")
|
111 |
progress_bar.empty()
|
112 |
|
113 |
except Exception as e:
|
114 |
+
st.error(f"Error loading image: {e}")
|
115 |
|
116 |
if __name__ == "__main__":
|
117 |
main()
|