Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,9 @@ import streamlit as st
|
|
2 |
import requests
|
3 |
import os
|
4 |
from PIL import Image
|
|
|
|
|
|
|
5 |
|
6 |
# Streamlit app title
|
7 |
st.title("Background Remover App")
|
@@ -23,7 +26,12 @@ if st.button("Start Processing"):
|
|
23 |
st.error("Please upload at least one image.")
|
24 |
else:
|
25 |
progress_bar = st.progress(0)
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
27 |
for i, uploaded_file in enumerate(uploaded_files):
|
28 |
image = uploaded_file.read()
|
29 |
try:
|
@@ -38,19 +46,35 @@ if st.button("Start Processing"):
|
|
38 |
)
|
39 |
|
40 |
if response.status_code == 200:
|
41 |
-
output_file_path = f"{uploaded_file.name.split('.')[0]}_processed.{output_format}"
|
42 |
with open(output_file_path, "wb") as f:
|
43 |
f.write(response.content)
|
44 |
|
45 |
-
|
46 |
st.success(f"Processed: {uploaded_file.name}")
|
47 |
-
st.image(Image.open(output_file_path), caption=f"Processed: {
|
48 |
else:
|
49 |
st.error(f"Failed to process {uploaded_file.name}: {response.json()}")
|
50 |
except Exception as e:
|
51 |
st.error(f"Error processing {uploaded_file.name}: {e}")
|
52 |
progress_bar.progress((i + 1) / len(uploaded_files))
|
53 |
|
54 |
-
if
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import requests
|
3 |
import os
|
4 |
from PIL import Image
|
5 |
+
import shutil
|
6 |
+
import zipfile
|
7 |
+
from io import BytesIO
|
8 |
|
9 |
# Streamlit app title
|
10 |
st.title("Background Remover App")
|
|
|
26 |
st.error("Please upload at least one image.")
|
27 |
else:
|
28 |
progress_bar = st.progress(0)
|
29 |
+
processed_files = []
|
30 |
+
|
31 |
+
# Create a temporary directory to store processed files
|
32 |
+
temp_dir = "processed_images"
|
33 |
+
os.makedirs(temp_dir, exist_ok=True)
|
34 |
+
|
35 |
for i, uploaded_file in enumerate(uploaded_files):
|
36 |
image = uploaded_file.read()
|
37 |
try:
|
|
|
46 |
)
|
47 |
|
48 |
if response.status_code == 200:
|
49 |
+
output_file_path = os.path.join(temp_dir, f"{uploaded_file.name.split('.')[0]}_processed.{output_format}")
|
50 |
with open(output_file_path, "wb") as f:
|
51 |
f.write(response.content)
|
52 |
|
53 |
+
processed_files.append(output_file_path)
|
54 |
st.success(f"Processed: {uploaded_file.name}")
|
55 |
+
st.image(Image.open(output_file_path), caption=f"Processed: {uploaded_file.name}")
|
56 |
else:
|
57 |
st.error(f"Failed to process {uploaded_file.name}: {response.json()}")
|
58 |
except Exception as e:
|
59 |
st.error(f"Error processing {uploaded_file.name}: {e}")
|
60 |
progress_bar.progress((i + 1) / len(uploaded_files))
|
61 |
|
62 |
+
if processed_files:
|
63 |
+
# Create a zip archive of all processed images
|
64 |
+
zip_buffer = BytesIO()
|
65 |
+
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
|
66 |
+
for file_path in processed_files:
|
67 |
+
zip_file.write(file_path, os.path.basename(file_path))
|
68 |
+
zip_buffer.seek(0)
|
69 |
|
70 |
+
# Provide download link for the zip file
|
71 |
+
st.success("Processing completed!")
|
72 |
+
st.download_button(
|
73 |
+
label="Download All Processed Images as ZIP",
|
74 |
+
data=zip_buffer,
|
75 |
+
file_name="processed_images.zip",
|
76 |
+
mime="application/zip"
|
77 |
+
)
|
78 |
+
|
79 |
+
# Cleanup temporary directory
|
80 |
+
shutil.rmtree(temp_dir)
|