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 # Streamlit UI st.title("Face Filtering App") # Modal Popup for Instructions 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." ) # Instructions sidebar 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." ) # Image uploader 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 = [] # Create a progress bar progress_bar = st.progress(0) # Show a spinner while processing images for idx, uploaded_file in enumerate(uploaded_files): image = Image.open(uploaded_file) # Show original image if only one file is uploaded if len(uploaded_files) == 1: st.image(image, caption=f"Original Image: {uploaded_file.name}", use_column_width=True) # Apply filters 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) # Save each filtered image to a temporary in-memory buffer buffered = io.BytesIO() filtered_image.save(buffered, format="PNG") processed_images.append((f"{uploaded_file.name.split('.')[0]}_{filter_name}.png", buffered.getvalue())) # Update progress bar progress = (idx + 1) / len(uploaded_files) progress_bar.progress(progress) # Simulate processing time time.sleep(0.1) # Create a zip file for download 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) # Provide a download button for the zip file st.download_button( label="Download Processed Images", data=zip_buffer, file_name="processed_images.zip", mime="application/zip" ) # Show balloon pop-up when processing is complete st.balloons() else: st.warning("Please upload at least one image.") # Copyright notice st.markdown( """