File size: 2,178 Bytes
f26700b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from PIL import Image, ImageOps, ImageEnhance
import numpy as np
import cv2
import time

# Function for sepia filter
def apply_sepia(image):
    img = np.array(image)
    sepia_filter = np.array([[0.393, 0.769, 0.189],
                             [0.349, 0.686, 0.168],
                             [0.272, 0.534, 0.131]])
    sepia_image = cv2.transform(img, sepia_filter)
    sepia_image = np.clip(sepia_image, 0, 255)
    return Image.fromarray(sepia_image.astype('uint8'))

# Function for edge detection
def apply_edge_detection(image):
    img = np.array(image.convert("L"))
    edges = cv2.Canny(img, 100, 200)
    return Image.fromarray(edges)

# App title
st.title("Advanced Image Filtering Animation")

# Sidebar for filter selection
st.sidebar.title("Choose Filter")
filters = st.sidebar.radio(
    "Select a filter:",
    ["Grayscale", "Sepia", "Edge Detection"]
)

# Upload image
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    image = Image.open(uploaded_file)
    st.image(image, caption="Original Image", use_column_width=True)

    # Button to start the animation
    if st.button("Apply Filter Animation"):
        st.write("Processing...")
        progress_bar = st.progress(0)

        # Animation logic based on selected filter
        for i in range(1, 11):
            if filters == "Grayscale":
                gray_image = ImageOps.grayscale(image)
                blended = Image.blend(image.convert("RGBA"), gray_image.convert("RGBA"), alpha=i/10)
            elif filters == "Sepia":
                sepia_image = apply_sepia(image)
                blended = Image.blend(image.convert("RGBA"), sepia_image.convert("RGBA"), alpha=i/10)
            elif filters == "Edge Detection":
                edge_image = apply_edge_detection(image)
                blended = Image.blend(image.convert("RGBA"), edge_image.convert("RGBA"), alpha=i/10)

            st.image(blended, caption=f"Step {i}/10", use_column_width=True)
            progress_bar.progress(i * 10)
            time.sleep(0.2)  # Simulate processing delay

        st.success("Animation Complete!")