Spaces:
Sleeping
Sleeping
File size: 4,811 Bytes
f05e029 6a3bfec b15e399 6a3bfec b15e399 6a3bfec d32093c 6a3bfec d32093c 6a3bfec d32093c 6a3bfec d32093c 6a3bfec c782af6 b15e399 c782af6 d32093c 6a3bfec d32093c 6a3bfec d32093c 6a3bfec c782af6 6a3bfec c782af6 6a3bfec d32093c 6a3bfec 6fb7620 d32093c 6a3bfec 6fb7620 6a3bfec 6fb7620 6a3bfec b15e399 6a3bfec b15e399 6a3bfec 6fb7620 6a3bfec d32093c 6a3bfec d32093c 6a3bfec d32093c 6a3bfec 6fb7620 6a3bfec d32093c 6a3bfec d32093c 6a3bfec |
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# Image Optimizer free for use no resell!
# Copyright by Volkan K-Budak
# Created for TSOWatch Commuinity
import streamlit as st
from PIL import Image
import io
import traceback
import time
def compress_image(image, format='webp', quality=85):
try:
# Korrektur: Wandle 'jpg' in 'jpeg' um
save_format = 'jpeg' if format.lower() == 'jpg' else format
img_byte_arr = io.BytesIO()
image.save(img_byte_arr, format=save_format.upper(), quality=quality)
img_byte_arr.seek(0)
return Image.open(img_byte_arr)
except Exception as e:
st.error(f"Image compression error: {e}")
st.error(traceback.format_exc())
return None
def main():
st.title("Image optimization tool")
st.sidebar.header("Optimization settings")
uploaded_file = st.file_uploader("Select an image", type=['jpg', 'png', 'jpeg', 'webp'])
if uploaded_file is not None:
try:
original_image = Image.open(uploaded_file)
# Aktuelles Format ermitteln
current_format = original_image.format.lower() if original_image.format else uploaded_file.name.split('.')[-1].lower()
current_format = 'jpeg' if current_format == 'jpg' else current_format
st.subheader("Original image")
st.image(original_image, caption=f"Original image ({current_format.upper()})")
original_size_bytes = len(uploaded_file.getvalue())
st.write(f"Original image size: {original_size_bytes} Bytes")
compression_quality = st.sidebar.slider(
"Compression quality",
min_value=1,
max_value=100,
value=85
)
# Zielformate ohne aktuelles Format
target_formats = [fmt for fmt in ['webp', 'jpg', 'png'] if fmt != current_format]
target_format = st.sidebar.selectbox(
"Zielformat",
target_formats
)
st.warning("⚠️ Please click 'Optimize Image' ONLY ONCE'!")
progress_bar = st.progress(0)
if st.button("Optimize image"):
try:
progress_bar.progress(20)
time.sleep(0.5)
optimized_image = compress_image(
original_image,
format=target_format,
quality=compression_quality
)
progress_bar.progress(60)
time.sleep(0.5)
if optimized_image:
# Korrektur: Speichern mit korrektem Format
save_format = 'jpeg' if target_format.lower() == 'jpg' else target_format
img_byte_arr = io.BytesIO()
optimized_image.save(img_byte_arr, format=save_format.upper(), quality=compression_quality)
img_byte_arr.seek(0)
progress_bar.progress(100)
time.sleep(0.5)
optimized_size_bytes = img_byte_arr.getbuffer().nbytes
compression_ratio = (1 - optimized_size_bytes / original_size_bytes) * 100
st.subheader("Optimization results")
col1, col2 = st.columns(2)
with col1:
st.image(optimized_image, caption=f"Optimized ({target_format.upper()})")
with col2:
st.write(f"Original size: {original_size_bytes} Bytes")
st.write(f"Optimized size: {optimized_size_bytes} Bytes")
st.write(f"Compression rate: {compression_ratio:.2f}%")
st.download_button(
label=f"Download {target_format.upper()}",
data=img_byte_arr,
file_name=f"optimized_image.{target_format}",
mime=f"image/{target_format}"
)
progress_bar.empty()
except Exception as e:
st.error(f"Image optimization error: {e}")
progress_bar.empty()
except Exception as e:
st.error(f"Error loading image: {e}")
if __name__ == "__main__":
main() |