Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
img_byte_arr = io.BytesIO()
|
10 |
+
image.save(img_byte_arr, format=format.upper(), quality=quality)
|
11 |
+
img_byte_arr.seek(0)
|
12 |
+
return Image.open(img_byte_arr)
|
13 |
+
except Exception as e:
|
14 |
+
st.error(f"Fehler bei Bildkompression: {e}")
|
15 |
+
st.error(traceback.format_exc())
|
16 |
+
return None
|
17 |
+
|
18 |
+
def main():
|
19 |
+
st.title("Bild Optimierungs-Tool")
|
20 |
+
|
21 |
+
st.sidebar.header("Optimierungseinstellungen")
|
22 |
+
|
23 |
+
uploaded_file = st.file_uploader("Wählen Sie ein Bild", type=['jpg', 'png', 'jpeg', 'webp'])
|
24 |
+
|
25 |
+
if uploaded_file is not None:
|
26 |
+
try:
|
27 |
+
original_image = Image.open(uploaded_file)
|
28 |
+
|
29 |
+
st.subheader("Originalbild")
|
30 |
+
st.image(original_image, caption="Ursprüngliches Bild")
|
31 |
+
|
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 |
+
target_format = st.sidebar.selectbox(
|
43 |
+
"Zielformat",
|
44 |
+
['webp', 'jpg', 'png']
|
45 |
+
)
|
46 |
+
|
47 |
+
# Status-Flag für Optimierung
|
48 |
+
if 'optimization_done' not in st.session_state:
|
49 |
+
st.session_state.optimization_done = False
|
50 |
+
|
51 |
+
if st.button("Bild optimieren", disabled=st.session_state.optimization_done):
|
52 |
+
# Setze Flag, um mehrfache Optimierung zu verhindern
|
53 |
+
st.session_state.optimization_done = True
|
54 |
+
|
55 |
+
# Fortschrittsbalken
|
56 |
+
progress_bar = st.progress(0)
|
57 |
+
status_text = st.empty()
|
58 |
+
|
59 |
+
try:
|
60 |
+
# Simuliere Optimierungsprozess
|
61 |
+
status_text.text("Optimierung startet...")
|
62 |
+
progress_bar.progress(20)
|
63 |
+
time.sleep(1)
|
64 |
+
|
65 |
+
optimized_image = compress_image(
|
66 |
+
original_image,
|
67 |
+
format=target_format,
|
68 |
+
quality=compression_quality
|
69 |
+
)
|
70 |
+
|
71 |
+
status_text.text("Bildverarbeitung...")
|
72 |
+
progress_bar.progress(50)
|
73 |
+
time.sleep(1)
|
74 |
+
|
75 |
+
if optimized_image:
|
76 |
+
img_byte_arr = io.BytesIO()
|
77 |
+
optimized_image.save(img_byte_arr, format=target_format.upper(), quality=compression_quality)
|
78 |
+
img_byte_arr.seek(0)
|
79 |
+
|
80 |
+
status_text.text("Finalisiere...")
|
81 |
+
progress_bar.progress(80)
|
82 |
+
time.sleep(1)
|
83 |
+
|
84 |
+
optimized_size_bytes = img_byte_arr.getbuffer().nbytes
|
85 |
+
compression_ratio = (1 - optimized_size_bytes / original_size_bytes) * 100
|
86 |
+
|
87 |
+
progress_bar.progress(100)
|
88 |
+
status_text.text("Optimierung abgeschlossen!")
|
89 |
+
|
90 |
+
st.subheader("Optimierungsergebnisse")
|
91 |
+
col1, col2 = st.columns(2)
|
92 |
+
|
93 |
+
with col1:
|
94 |
+
st.image(optimized_image, caption=f"Optimiert ({target_format.upper()})")
|
95 |
+
|
96 |
+
with col2:
|
97 |
+
st.write(f"Ursprüngliche Größe: {original_size_bytes} Bytes")
|
98 |
+
st.write(f"Optimierte Größe: {optimized_size_bytes} Bytes")
|
99 |
+
st.write(f"Kompressionsrate: {compression_ratio:.2f}%")
|
100 |
+
|
101 |
+
st.download_button(
|
102 |
+
label=f"Download {target_format.upper()}",
|
103 |
+
data=img_byte_arr,
|
104 |
+
file_name=f"optimized_image.{target_format}",
|
105 |
+
mime=f"image/{target_format}"
|
106 |
+
)
|
107 |
+
|
108 |
+
except Exception as e:
|
109 |
+
st.error(f"Fehler bei Bildoptimierung: {e}")
|
110 |
+
# Setze Flag zurück bei Fehler
|
111 |
+
st.session_state.optimization_done = False
|
112 |
+
|
113 |
+
finally:
|
114 |
+
# Lösche Fortschrittsbalken und Statustext
|
115 |
+
progress_bar.empty()
|
116 |
+
status_text.empty()
|
117 |
+
|
118 |
+
except Exception as e:
|
119 |
+
st.error(f"Fehler beim Laden des Bildes: {e}")
|
120 |
+
|
121 |
+
if __name__ == "__main__":
|
122 |
+
main()
|