Alibrown commited on
Commit
d32093c
·
verified ·
1 Parent(s): b15e399

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
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"Fehler bei Bildkompression: {e}")
18
  st.error(traceback.format_exc())
19
  return None
20
 
21
  def main():
22
- st.title("Bild Optimierungs-Tool")
23
 
24
- st.sidebar.header("Optimierungseinstellungen")
25
 
26
- uploaded_file = st.file_uploader("Wählen Sie ein Bild", type=['jpg', 'png', 'jpeg', 'webp'])
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("Originalbild")
37
- st.image(original_image, caption=f"Ursprüngliches Bild ({current_format.upper()})")
38
 
39
  original_size_bytes = len(uploaded_file.getvalue())
40
- st.write(f"Ursprüngliche Bildgröße: {original_size_bytes} Bytes")
41
 
42
  compression_quality = st.sidebar.slider(
43
- "Kompressionsqualität",
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("⚠️ Bitte klicken Sie NUR EINMAL auf 'Bild optimieren'!")
58
 
59
  progress_bar = st.progress(0)
60
 
61
- if st.button("Bild optimieren"):
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("Optimierungsergebnisse")
90
  col1, col2 = st.columns(2)
91
 
92
  with col1:
93
- st.image(optimized_image, caption=f"Optimiert ({target_format.upper()})")
94
 
95
  with col2:
96
- st.write(f"Ursprüngliche Größe: {original_size_bytes} Bytes")
97
- st.write(f"Optimierte Größe: {optimized_size_bytes} Bytes")
98
- st.write(f"Kompressionsrate: {compression_ratio:.2f}%")
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"Fehler bei Bildoptimierung: {e}")
111
  progress_bar.empty()
112
 
113
  except Exception as e:
114
- st.error(f"Fehler beim Laden des Bildes: {e}")
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()