kartik2627's picture
Create app.py
f26700b verified
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!")