Alibrown commited on
Commit
55cfc4b
·
verified ·
1 Parent(s): 50b96cc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import io
4
+ import traceback
5
+ import time
6
+
7
+ def compress_image(image, format='webp', quality=85):
8
+ try:
9
+ # Korrektur: Wandle 'jpg' in 'jpeg' um
10
+ save_format = 'jpeg' if format.lower() == 'jpg' else format
11
+
12
+ # Korrektur: Quality-Wert wird umgekehrt angewendet
13
+ adjusted_quality = 101 - quality # Dies dreht die Skala um
14
+
15
+ img_byte_arr = io.BytesIO()
16
+ image.save(img_byte_arr, format=save_format.upper(), quality=adjusted_quality)
17
+ img_byte_arr.seek(0)
18
+ return Image.open(img_byte_arr)
19
+ except Exception as e:
20
+ st.error(f"Image compression error: {e}")
21
+ st.error(traceback.format_exc())
22
+ return None
23
+
24
+ def main():
25
+ st.title("Image optimization tool")
26
+
27
+ st.sidebar.header("Optimization settings")
28
+
29
+ uploaded_file = st.file_uploader("Select an image", type=['jpg', 'png', 'jpeg', 'webp'])
30
+
31
+ if uploaded_file is not None:
32
+ try:
33
+ original_image = Image.open(uploaded_file)
34
+
35
+ current_format = original_image.format.lower() if original_image.format else uploaded_file.name.split('.')[-1].lower()
36
+ current_format = 'jpeg' if current_format == 'jpg' else current_format
37
+
38
+ st.subheader("Original image")
39
+ st.image(original_image, caption=f"Original image ({current_format.upper()})")
40
+
41
+ original_size_bytes = len(uploaded_file.getvalue())
42
+ st.write(f"Original image size: {original_size_bytes} Bytes")
43
+
44
+ compression_quality = st.sidebar.slider(
45
+ "Compression quality (100 = best quality, 1 = smallest file)",
46
+ min_value=1,
47
+ max_value=100,
48
+ value=85
49
+ )
50
+
51
+ target_formats = [fmt for fmt in ['webp', 'jpg', 'png'] if fmt != current_format]
52
+
53
+ target_format = st.sidebar.selectbox(
54
+ "Target format",
55
+ target_formats
56
+ )
57
+
58
+ st.warning("⚠️ Please click 'Optimize Image' ONLY ONCE'!")
59
+
60
+ progress_bar = st.progress(0)
61
+
62
+ if st.button("Optimize image"):
63
+ try:
64
+ progress_bar.progress(20)
65
+ time.sleep(0.5)
66
+
67
+ optimized_image = compress_image(
68
+ original_image,
69
+ format=target_format,
70
+ quality=compression_quality
71
+ )
72
+
73
+ progress_bar.progress(60)
74
+ time.sleep(0.5)
75
+
76
+ if optimized_image:
77
+ save_format = 'jpeg' if target_format.lower() == 'jpg' else target_format
78
+
79
+ img_byte_arr = io.BytesIO()
80
+ optimized_image.save(img_byte_arr, format=save_format.upper(), quality=101-compression_quality)
81
+ img_byte_arr.seek(0)
82
+
83
+ progress_bar.progress(100)
84
+ time.sleep(0.5)
85
+
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()}",
102
+ data=img_byte_arr,
103
+ file_name=f"optimized_image.{target_format}",
104
+ mime=f"image/{target_format}"
105
+ )
106
+
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()