Alibrown commited on
Commit
857baf6
·
verified ·
1 Parent(s): a3a633e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import io
4
+ import numpy as np
5
+ from image_optimization_utils import (
6
+ compress_image,
7
+ convert_image_format,
8
+ resize_image
9
+ )
10
+
11
+ def main():
12
+ st.title("Bild Optimierungs-Tool")
13
+
14
+ # Seitenleiste für Einstellungen
15
+ st.sidebar.header("Optimierungseinstellungen")
16
+
17
+ # Datei-Upload
18
+ uploaded_file = st.file_uploader("Wählen Sie ein Bild", type=['jpg', 'png', 'jpeg', 'webp'])
19
+
20
+ if uploaded_file is not None:
21
+ # Bild laden
22
+ original_image = Image.open(uploaded_file)
23
+
24
+ # Vorschau des Originalbildes
25
+ st.subheader("Originalbild")
26
+ st.image(original_image, caption="Ursprüngliches Bild")
27
+
28
+ # Kompressionseinstellungen
29
+ compression_quality = st.sidebar.slider(
30
+ "Kompressionsqualität",
31
+ min_value=1,
32
+ max_value=100,
33
+ value=85
34
+ )
35
+
36
+ # Zielformate
37
+ target_formats = st.sidebar.multiselect(
38
+ "Zielformate",
39
+ ['jpg', 'png', 'webp'],
40
+ default=['webp']
41
+ )
42
+
43
+ # Optimierungsbutton
44
+ if st.button("Bild optimieren"):
45
+ # Optimierungsergebnisse
46
+ results = {}
47
+
48
+ for format in target_formats:
49
+ # Bild komprimieren und konvertieren
50
+ optimized_image = convert_image_format(
51
+ original_image,
52
+ target_format=format,
53
+ quality=compression_quality
54
+ )
55
+
56
+ # Speichern im Speicher
57
+ img_byte_arr = io.BytesIO()
58
+ optimized_image.save(img_byte_arr, format=format.upper(), quality=compression_quality)
59
+ img_byte_arr = img_byte_arr.getvalue()
60
+
61
+ # Größenberechnung
62
+ original_size = len(uploaded_file.getvalue())
63
+ optimized_size = len(img_byte_arr)
64
+ compression_ratio = (1 - optimized_size / original_size) * 100
65
+
66
+ # Ergebnisse speichern
67
+ results[format] = {
68
+ 'image': optimized_image,
69
+ 'original_size': original_size,
70
+ 'optimized_size': optimized_size,
71
+ 'compression_ratio': compression_ratio
72
+ }
73
+
74
+ # Ergebnisse anzeigen
75
+ st.subheader("Optimierungsergebnisse")
76
+ for format, result in results.items():
77
+ col1, col2 = st.columns(2)
78
+
79
+ with col1:
80
+ st.image(result['image'], caption=f"Optimiert ({format.upper()})")
81
+
82
+ with col2:
83
+ st.write(f"Ursprüngliche Größe: {result['original_size']} Bytes")
84
+ st.write(f"Optimierte Größe: {result['optimized_size']} Bytes")
85
+ st.write(f"Kompressionsrate: {result['compression_ratio']:.2f}%")
86
+
87
+ # Download-Button
88
+ img_byte_arr = io.BytesIO()
89
+ result['image'].save(img_byte_arr, format=format.upper(), quality=compression_quality)
90
+ st.download_button(
91
+ label=f"Download {format.upper()}",
92
+ data=img_byte_arr.getvalue(),
93
+ file_name=f"optimized_image.{format}",
94
+ mime=f"image/{format}"
95
+ )
96
+
97
+ # Hilfsfunktionen in separater Datei image_optimization_utils.py