Spaces:
Sleeping
Sleeping
Delete top.py
Browse files
top.py
DELETED
@@ -1,97 +0,0 @@
|
|
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")
|
19 |
-
|
20 |
-
st.sidebar.header("Optimierungseinstellungen")
|
21 |
-
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|