|
import streamlit as st |
|
from PIL import Image, ImageFilter |
|
import io |
|
import zipfile |
|
import time |
|
|
|
def apply_filters(image): |
|
"""Apply various filters to the image.""" |
|
filters = { |
|
"Detail": ImageFilter.DETAIL, |
|
"Edge Enhance": ImageFilter.EDGE_ENHANCE, |
|
"Edge Enhance More": ImageFilter.EDGE_ENHANCE_MORE, |
|
"Smooth": ImageFilter.SMOOTH, |
|
"Smooth More": ImageFilter.SMOOTH_MORE, |
|
"Sharpen": ImageFilter.SHARPEN |
|
} |
|
|
|
filtered_images = {} |
|
for filter_name, filter_type in filters.items(): |
|
filtered_images[filter_name] = image.filter(filter_type) |
|
return filtered_images |
|
|
|
|
|
st.title("Face Filtering App") |
|
|
|
|
|
with st.expander("Learn about Face Filtering", expanded=False): |
|
st.write( |
|
"Face filtering is a creative technique that enhances images by applying various effects to highlight different features of the subject. The filters available in this app include Detail, which sharpens textures and makes features stand out; Edge Enhance and Edge Enhance More, which accentuate edges for a defined look; Smooth and Smooth More, which soften the image and reduce imperfections for a polished appearance; and Sharpen, which increases contrast and clarity to reveal finer details. Together, these filters allow users to transform their photos, emphasizing beauty and enabling unique artistic interpretations, making face filtering an engaging way to manipulate images and express creativity." |
|
) |
|
|
|
|
|
with st.sidebar: |
|
st.header("Instructions") |
|
st.write( |
|
"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" |
|
"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." |
|
) |
|
|
|
|
|
uploaded_files = st.file_uploader("Choose images", accept_multiple_files=True, type=["jpg", "jpeg", "png"]) |
|
|
|
if st.button("Process Images"): |
|
if uploaded_files: |
|
processed_images = [] |
|
|
|
|
|
progress_bar = st.progress(0) |
|
|
|
|
|
for idx, uploaded_file in enumerate(uploaded_files): |
|
image = Image.open(uploaded_file) |
|
|
|
|
|
if len(uploaded_files) == 1: |
|
st.image(image, caption=f"Original Image: {uploaded_file.name}", use_column_width=True) |
|
|
|
|
|
filtered_images = apply_filters(image) |
|
for filter_name, filtered_image in filtered_images.items(): |
|
if len(uploaded_files) == 1: |
|
st.image(filtered_image, caption=f"{filter_name} Filter", use_column_width=True) |
|
|
|
|
|
buffered = io.BytesIO() |
|
filtered_image.save(buffered, format="PNG") |
|
processed_images.append((f"{uploaded_file.name.split('.')[0]}_{filter_name}.png", buffered.getvalue())) |
|
|
|
|
|
progress = (idx + 1) / len(uploaded_files) |
|
progress_bar.progress(progress) |
|
|
|
|
|
time.sleep(0.1) |
|
|
|
|
|
zip_buffer = io.BytesIO() |
|
with zipfile.ZipFile(zip_buffer, 'w') as zip_file: |
|
for filename, img_data in processed_images: |
|
zip_file.writestr(filename, img_data) |
|
|
|
zip_buffer.seek(0) |
|
|
|
|
|
st.download_button( |
|
label="Download Processed Images", |
|
data=zip_buffer, |
|
file_name="processed_images.zip", |
|
mime="application/zip" |
|
) |
|
|
|
|
|
st.balloons() |
|
|
|
else: |
|
st.warning("Please upload at least one image.") |
|
|
|
|
|
st.markdown( |
|
""" |
|
<style> |
|
.copyright { |
|
position: fixed; |
|
bottom: 10px; |
|
right: 10px; |
|
font-size: 18px; |
|
color: #0F81C9; |
|
} |
|
</style> |
|
<div class="copyright"> |
|
Copyright: [email protected] |
|
</div> |
|
""", |
|
unsafe_allow_html=True |
|
) |
|
|