Alibrown commited on
Commit
207eb38
·
verified ·
1 Parent(s): 1cfc13a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -71
app.py CHANGED
@@ -1,18 +1,18 @@
1
  import streamlit as st
2
  from PIL import Image
3
  import io
 
4
 
5
- def compress_image(image, quality=85):
6
- img_byte_arr = io.BytesIO()
7
- image.save(img_byte_arr, format='JPEG', quality=quality)
8
- img_byte_arr.seek(0)
9
- return Image.open(img_byte_arr)
10
-
11
- def convert_image_format(image, target_format='webp', quality=85):
12
- img_byte_arr = io.BytesIO()
13
- image.save(img_byte_arr, format=target_format.upper(), quality=quality)
14
- img_byte_arr.seek(0)
15
- return Image.open(img_byte_arr)
16
 
17
  def main():
18
  st.title("Bild Optimierungs-Tool")
@@ -22,69 +22,76 @@ def main():
22
  uploaded_file = st.file_uploader("Wählen Sie ein Bild", type=['jpg', 'png', 'jpeg', 'webp'])
23
 
24
  if uploaded_file is not None:
25
- original_image = Image.open(uploaded_file)
26
-
27
- st.subheader("Originalbild")
28
- st.image(original_image, caption="Ursprüngliches Bild")
29
-
30
- compression_quality = st.sidebar.slider(
31
- "Kompressionsqualität",
32
- min_value=1,
33
- max_value=100,
34
- value=85
35
- )
36
-
37
- target_formats = st.sidebar.multiselect(
38
- "Zielformate",
39
- ['jpg', 'png', 'webp'],
40
- default=['webp']
41
- )
42
-
43
- if st.button("Bild optimieren"):
44
- results = {}
45
 
46
- for format in target_formats:
47
- optimized_image = convert_image_format(
48
- original_image,
49
- target_format=format,
50
- quality=compression_quality
51
- )
52
-
53
- img_byte_arr = io.BytesIO()
54
- optimized_image.save(img_byte_arr, format=format.upper(), quality=compression_quality)
55
- img_byte_arr = img_byte_arr.getvalue()
56
-
57
- original_size = len(uploaded_file.getvalue())
58
- optimized_size = len(img_byte_arr)
59
- compression_ratio = (1 - optimized_size / original_size) * 100
60
-
61
- results[format] = {
62
- 'image': optimized_image,
63
- 'original_size': original_size,
64
- 'optimized_size': optimized_size,
65
- 'compression_ratio': compression_ratio
66
- }
67
 
68
- st.subheader("Optimierungsergebnisse")
69
- for format, result in results.items():
70
- col1, col2 = st.columns(2)
71
-
72
- with col1:
73
- st.image(result['image'], caption=f"Optimiert ({format.upper()})")
74
-
75
- with col2:
76
- st.write(f"Ursprüngliche Größe: {result['original_size']} Bytes")
77
- st.write(f"Optimierte Größe: {result['optimized_size']} Bytes")
78
- st.write(f"Kompressionsrate: {result['compression_ratio']:.2f}%")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
- img_byte_arr = io.BytesIO()
81
- result['image'].save(img_byte_arr, format=format.upper(), quality=compression_quality)
82
- st.download_button(
83
- label=f"Download {format.upper()}",
84
- data=img_byte_arr.getvalue(),
85
- file_name=f"optimized_image.{format}",
86
- mime=f"image/{format}"
87
- )
88
 
89
  if __name__ == "__main__":
90
  main()
 
1
  import streamlit as st
2
  from PIL import Image
3
  import io
4
+ import traceback
5
 
6
+ def compress_image(image, format='webp', quality=85):
7
+ try:
8
+ img_byte_arr = io.BytesIO()
9
+ image.save(img_byte_arr, format=format.upper(), quality=quality)
10
+ img_byte_arr.seek(0)
11
+ return Image.open(img_byte_arr)
12
+ except Exception as e:
13
+ st.error(f"Fehler bei Bildkompression: {e}")
14
+ st.error(traceback.format_exc())
15
+ return None
 
16
 
17
  def main():
18
  st.title("Bild Optimierungs-Tool")
 
22
  uploaded_file = st.file_uploader("Wählen Sie ein Bild", type=['jpg', 'png', 'jpeg', 'webp'])
23
 
24
  if uploaded_file is not None:
25
+ try:
26
+ original_image = Image.open(uploaded_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
+ st.subheader("Originalbild")
29
+ st.image(original_image, caption="Ursprüngliches Bild")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ # Ursprungsgröße anzeigen
32
+ original_size_bytes = len(uploaded_file.getvalue())
33
+ st.write(f"Ursprüngliche Bildgröße: {original_size_bytes} Bytes")
34
+
35
+ compression_quality = st.sidebar.slider(
36
+ "Kompressionsqualität",
37
+ min_value=1,
38
+ max_value=100,
39
+ value=85
40
+ )
41
+
42
+ # Nur ein Format wählbar
43
+ target_format = st.sidebar.selectbox(
44
+ "Zielformat",
45
+ ['webp', 'jpg', 'png']
46
+ )
47
+
48
+ if st.button("Bild optimieren"):
49
+ try:
50
+ # Bild optimieren
51
+ optimized_image = compress_image(
52
+ original_image,
53
+ format=target_format,
54
+ quality=compression_quality
55
+ )
56
+
57
+ if optimized_image:
58
+ # Speichern im Speicher
59
+ img_byte_arr = io.BytesIO()
60
+ optimized_image.save(img_byte_arr, format=target_format.upper(), quality=compression_quality)
61
+ img_byte_arr.seek(0)
62
+
63
+ # Größenvergleich
64
+ optimized_size_bytes = img_byte_arr.getbuffer().nbytes
65
+ compression_ratio = (1 - optimized_size_bytes / original_size_bytes) * 100
66
+
67
+ st.subheader("Optimierungsergebnisse")
68
+ col1, col2 = st.columns(2)
69
+
70
+ with col1:
71
+ st.image(optimized_image, caption=f"Optimiert ({target_format.upper()})")
72
+
73
+ with col2:
74
+ st.write(f"Ursprüngliche Größe: {original_size_bytes} Bytes")
75
+ st.write(f"Optimierte Größe: {optimized_size_bytes} Bytes")
76
+ st.write(f"Kompressionsrate: {compression_ratio:.2f}%")
77
+
78
+ # Download-Button
79
+ st.download_button(
80
+ label=f"Download {target_format.upper()}",
81
+ data=img_byte_arr,
82
+ file_name=f"optimized_image.{target_format}",
83
+ mime=f"image/{target_format}"
84
+ )
85
+ else:
86
+ st.error("Bildoptimierung fehlgeschlagen")
87
 
88
+ except Exception as e:
89
+ st.error(f"Fehler bei Bildoptimierung: {e}")
90
+ st.error(traceback.format_exc())
91
+
92
+ except Exception as e:
93
+ st.error(f"Fehler beim Laden des Bildes: {e}")
94
+ st.error(traceback.format_exc())
 
95
 
96
  if __name__ == "__main__":
97
  main()