Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image, ImageFilter
|
3 |
+
import io
|
4 |
+
import zipfile
|
5 |
+
import time
|
6 |
+
|
7 |
+
def apply_filters(image):
|
8 |
+
"""Apply various filters to the image."""
|
9 |
+
filters = {
|
10 |
+
"Detail": ImageFilter.DETAIL,
|
11 |
+
"Edge Enhance": ImageFilter.EDGE_ENHANCE,
|
12 |
+
"Edge Enhance More": ImageFilter.EDGE_ENHANCE_MORE,
|
13 |
+
"Smooth": ImageFilter.SMOOTH,
|
14 |
+
"Smooth More": ImageFilter.SMOOTH_MORE,
|
15 |
+
"Sharpen": ImageFilter.SHARPEN
|
16 |
+
}
|
17 |
+
|
18 |
+
filtered_images = {}
|
19 |
+
for filter_name, filter_type in filters.items():
|
20 |
+
filtered_images[filter_name] = image.filter(filter_type)
|
21 |
+
return filtered_images
|
22 |
+
|
23 |
+
# Streamlit UI
|
24 |
+
st.title("Face Filtering App")
|
25 |
+
|
26 |
+
# Instructions sidebar
|
27 |
+
with st.sidebar:
|
28 |
+
st.header("Instructions")
|
29 |
+
st.write(
|
30 |
+
"1) If you choose only one image to upload, the results will be shown on display, and you can click the 'Download Processed Images' button to download the processed images in zip format.\n"
|
31 |
+
"2) If you choose more than one image, it will not display any image as results; instead, it will directly show you the 'Download Processed Images' button, and you can click on it to download the processed images in zip format."
|
32 |
+
)
|
33 |
+
|
34 |
+
uploaded_files = st.file_uploader("Choose images", accept_multiple_files=True, type=["jpg", "jpeg", "png"])
|
35 |
+
|
36 |
+
if st.button("Process Images"):
|
37 |
+
if uploaded_files:
|
38 |
+
processed_images = []
|
39 |
+
|
40 |
+
# Create a progress bar
|
41 |
+
progress_bar = st.progress(0)
|
42 |
+
|
43 |
+
# Show a spinner while processing images
|
44 |
+
for idx, uploaded_file in enumerate(uploaded_files):
|
45 |
+
image = Image.open(uploaded_file)
|
46 |
+
|
47 |
+
# Show original image if only one file is uploaded
|
48 |
+
if len(uploaded_files) == 1:
|
49 |
+
st.image(image, caption=f"Original Image: {uploaded_file.name}", use_column_width=True)
|
50 |
+
|
51 |
+
# Apply filters
|
52 |
+
filtered_images = apply_filters(image)
|
53 |
+
for filter_name, filtered_image in filtered_images.items():
|
54 |
+
if len(uploaded_files) == 1:
|
55 |
+
st.image(filtered_image, caption=f"{filter_name} Filter", use_column_width=True)
|
56 |
+
|
57 |
+
# Save each filtered image to a temporary in-memory buffer
|
58 |
+
buffered = io.BytesIO()
|
59 |
+
filtered_image.save(buffered, format="PNG")
|
60 |
+
processed_images.append((f"{uploaded_file.name.split('.')[0]}_{filter_name}.png", buffered.getvalue()))
|
61 |
+
|
62 |
+
# Update progress bar
|
63 |
+
progress = (idx + 1) / len(uploaded_files)
|
64 |
+
progress_bar.progress(progress)
|
65 |
+
|
66 |
+
# Simulate processing time
|
67 |
+
time.sleep(0.1)
|
68 |
+
|
69 |
+
# Create a zip file for download
|
70 |
+
zip_buffer = io.BytesIO()
|
71 |
+
with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
|
72 |
+
for filename, img_data in processed_images:
|
73 |
+
zip_file.writestr(filename, img_data)
|
74 |
+
|
75 |
+
zip_buffer.seek(0)
|
76 |
+
|
77 |
+
# Provide a download button for the zip file
|
78 |
+
st.download_button(
|
79 |
+
label="Download Processed Images",
|
80 |
+
data=zip_buffer,
|
81 |
+
file_name="processed_images.zip",
|
82 |
+
mime="application/zip"
|
83 |
+
)
|
84 |
+
|
85 |
+
# Show balloon pop-up when processing is complete
|
86 |
+
st.balloons()
|
87 |
+
|
88 |
+
else:
|
89 |
+
st.warning("Please upload at least one image.")
|
90 |
+
|
91 |
+
# Copyright notice
|
92 |
+
st.markdown(
|
93 |
+
"""
|
94 |
+
<style>
|
95 |
+
.copyright {
|
96 |
+
position: fixed;
|
97 |
+
bottom: 10px;
|
98 |
+
right: 10px;
|
99 |
+
font-size: 18px;
|
100 |
+
color: #0F81C9;
|
101 |
+
}
|
102 |
+
</style>
|
103 |
+
<div class="copyright">
|
104 |
+
Copyright: [email protected]
|
105 |
+
</div>
|
106 |
+
""",
|
107 |
+
unsafe_allow_html=True
|
108 |
+
)
|