Spaces:
Build error
Build error
| import streamlit as st | |
| from PIL import Image | |
| import io | |
| import os | |
| # Set page config | |
| st.set_page_config( | |
| page_title="Image Compressor", | |
| page_icon="๐๏ธ", | |
| layout="wide" | |
| ) | |
| def get_file_size(image_bytes): | |
| """Get file size in bytes""" | |
| return len(image_bytes) | |
| def format_size(size_bytes): | |
| """Format bytes to human readable format""" | |
| if size_bytes < 1024: | |
| return f"{size_bytes} B" | |
| elif size_bytes < 1024 * 1024: | |
| return f"{size_bytes / 1024:.1f} KB" | |
| else: | |
| return f"{size_bytes / (1024 * 1024):.1f} MB" | |
| def compress_image(image, quality): | |
| """Compress image with specified quality""" | |
| # Convert to RGB if necessary | |
| if image.mode != 'RGB': | |
| image = image.convert('RGB') | |
| # Save to bytes with specified quality | |
| output = io.BytesIO() | |
| image.save(output, format='JPEG', quality=quality, optimize=True) | |
| compressed_bytes = output.getvalue() | |
| return compressed_bytes | |
| def main(): | |
| st.title("๐๏ธ Image Compressor") | |
| st.markdown("Upload an image and adjust the compression quality to reduce file size.") | |
| # File uploader | |
| uploaded_file = st.file_uploader( | |
| "Choose an image file", | |
| type=['png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp'], | |
| help="Upload an image to compress" | |
| ) | |
| if uploaded_file is not None: | |
| # Display original image | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.subheader("Original Image") | |
| original_image = Image.open(uploaded_file) | |
| st.image(original_image, caption="Original", use_column_width=True) | |
| # Get original file size | |
| original_bytes = uploaded_file.getvalue() | |
| original_size = get_file_size(original_bytes) | |
| st.metric("Original Size", format_size(original_size)) | |
| with col2: | |
| st.subheader("Compressed Image") | |
| # Quality slider | |
| quality = st.slider( | |
| "Compression Quality", | |
| min_value=1, | |
| max_value=100, | |
| value=85, | |
| help="Lower values = smaller file size, higher compression" | |
| ) | |
| # Compress image | |
| compressed_bytes = compress_image(original_image, quality) | |
| compressed_size = get_file_size(compressed_bytes) | |
| # Calculate savings | |
| savings = original_size - compressed_size | |
| savings_percent = (savings / original_size) * 100 if original_size > 0 else 0 | |
| # Display compressed image | |
| compressed_image = Image.open(io.BytesIO(compressed_bytes)) | |
| st.image(compressed_image, caption=f"Compressed (Quality: {quality})", use_column_width=True) | |
| # Display metrics | |
| col_metric1, col_metric2, col_metric3 = st.columns(3) | |
| with col_metric1: | |
| st.metric("Compressed Size", format_size(compressed_size)) | |
| with col_metric2: | |
| st.metric("Size Reduction", format_size(savings)) | |
| with col_metric3: | |
| st.metric("Compression Ratio", f"{savings_percent:.1f}%") | |
| # Download button | |
| st.subheader("Download Compressed Image") | |
| # Get file extension | |
| file_extension = os.path.splitext(uploaded_file.name)[1].lower() | |
| if file_extension in ['.png', '.gif', '.bmp']: | |
| output_format = 'JPEG' | |
| else: | |
| output_format = 'JPEG' | |
| # Create download filename | |
| base_name = os.path.splitext(uploaded_file.name)[0] | |
| download_filename = f"{base_name}_compressed.jpg" | |
| st.download_button( | |
| label="Download Compressed Image", | |
| data=compressed_bytes, | |
| file_name=download_filename, | |
| mime="image/jpeg" | |
| ) | |
| # Additional info | |
| with st.expander("Compression Details"): | |
| st.write(f"**Original format:** {original_image.format}") | |
| st.write(f"**Original mode:** {original_image.mode}") | |
| st.write(f"**Original dimensions:** {original_image.size[0]} x {original_image.size[1]}") | |
| st.write(f"**Compressed format:** JPEG") | |
| st.write(f"**Quality setting:** {quality}") | |
| st.write(f"**Size reduction:** {format_size(savings)} ({savings_percent:.1f}%)") | |
| else: | |
| st.info("๐ Please upload an image file to get started!") | |
| # Show example | |
| st.markdown("### How to use:") | |
| st.markdown("1. **Upload an image** using the file uploader above") | |
| st.markdown("2. **Adjust the quality slider** to control compression") | |
| st.markdown("3. **Compare** the original and compressed images") | |
| st.markdown("4. **Download** the compressed image") | |
| if __name__ == "__main__": | |
| main() | |