Alibrown's picture
Create app.py
857baf6 verified
raw
history blame
3.54 kB
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