File size: 3,538 Bytes
857baf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import streamlit as st
from PIL import Image
import io
import numpy as np
from image_optimization_utils import (
    compress_image, 
    convert_image_format, 
    resize_image
)

def main():
    st.title("Bild Optimierungs-Tool")
    
    # Seitenleiste für Einstellungen
    st.sidebar.header("Optimierungseinstellungen")
    
    # Datei-Upload
    uploaded_file = st.file_uploader("Wählen Sie ein Bild", type=['jpg', 'png', 'jpeg', 'webp'])
    
    if uploaded_file is not None:
        # Bild laden
        original_image = Image.open(uploaded_file)
        
        # Vorschau des Originalbildes
        st.subheader("Originalbild")
        st.image(original_image, caption="Ursprüngliches Bild")
        
        # Kompressionseinstellungen
        compression_quality = st.sidebar.slider(
            "Kompressionsqualität", 
            min_value=1, 
            max_value=100, 
            value=85
        )
        
        # Zielformate
        target_formats = st.sidebar.multiselect(
            "Zielformate", 
            ['jpg', 'png', 'webp'],
            default=['webp']
        )
        
        # Optimierungsbutton
        if st.button("Bild optimieren"):
            # Optimierungsergebnisse
            results = {}
            
            for format in target_formats:
                # Bild komprimieren und konvertieren
                optimized_image = convert_image_format(
                    original_image, 
                    target_format=format, 
                    quality=compression_quality
                )
                
                # Speichern im Speicher
                img_byte_arr = io.BytesIO()
                optimized_image.save(img_byte_arr, format=format.upper(), quality=compression_quality)
                img_byte_arr = img_byte_arr.getvalue()
                
                # Größenberechnung
                original_size = len(uploaded_file.getvalue())
                optimized_size = len(img_byte_arr)
                compression_ratio = (1 - optimized_size / original_size) * 100
                
                # Ergebnisse speichern
                results[format] = {
                    'image': optimized_image,
                    'original_size': original_size,
                    'optimized_size': optimized_size,
                    'compression_ratio': compression_ratio
                }
            
            # Ergebnisse anzeigen
            st.subheader("Optimierungsergebnisse")
            for format, result in results.items():
                col1, col2 = st.columns(2)
                
                with col1:
                    st.image(result['image'], caption=f"Optimiert ({format.upper()})")
                
                with col2:
                    st.write(f"Ursprüngliche Größe: {result['original_size']} Bytes")
                    st.write(f"Optimierte Größe: {result['optimized_size']} Bytes")
                    st.write(f"Kompressionsrate: {result['compression_ratio']:.2f}%")
                
                # Download-Button
                img_byte_arr = io.BytesIO()
                result['image'].save(img_byte_arr, format=format.upper(), quality=compression_quality)
                st.download_button(
                    label=f"Download {format.upper()}",
                    data=img_byte_arr.getvalue(),
                    file_name=f"optimized_image.{format}",
                    mime=f"image/{format}"
                )

# Hilfsfunktionen in separater Datei image_optimization_utils.py