File size: 3,806 Bytes
4429d74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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")

# 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."
    )

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(
    """

    <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
)