Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import io
|
| 4 |
+
import zipfile
|
| 5 |
+
|
| 6 |
+
st.title("Bulk Image Compressor")
|
| 7 |
+
uploaded_files = st.file_uploader("Select images to compress", type=["jpg", "jpeg", "png", "gif", "bmp"], accept_multiple_files=True)
|
| 8 |
+
quality = st.slider("Compression quality", 1, 95, 80)
|
| 9 |
+
compress_button = st.button("Compress images")
|
| 10 |
+
compressed_images = []
|
| 11 |
+
download_link = ""
|
| 12 |
+
|
| 13 |
+
if compress_button and uploaded_files:
|
| 14 |
+
progress_bar = st.progress(0)
|
| 15 |
+
|
| 16 |
+
for i, file in enumerate(uploaded_files):
|
| 17 |
+
image = Image.open(io.BytesIO(file.getvalue()))
|
| 18 |
+
buffer = io.BytesIO()
|
| 19 |
+
image.save(buffer, format="JPEG", quality=quality)
|
| 20 |
+
compressed_image = buffer.getvalue()
|
| 21 |
+
compressed_images.append(compressed_image)
|
| 22 |
+
progress_bar.progress((i + 1) / len(uploaded_files))
|
| 23 |
+
|
| 24 |
+
zip_buffer = io.BytesIO()
|
| 25 |
+
with zipfile.ZipFile(zip_buffer, "w") as zip_file:
|
| 26 |
+
for i, compressed_image in enumerate(compressed_images):
|
| 27 |
+
zip_file.writestr(f"image_{i}.jpg", compressed_image)
|
| 28 |
+
|
| 29 |
+
zip_buffer.seek(0)
|
| 30 |
+
download_link = st.download_button("Download", zip_buffer.getvalue(), "compressed_images.zip", "application/zip")
|