Spaces:
Sleeping
Sleeping
# app.py | |
import streamlit as st | |
from PIL import Image, ImageOps, ImageFilter, ImageEnhance | |
import io | |
import os | |
st.set_page_config(page_title="Image Filter App", layout="wide") | |
st.title("πΌοΈ Fun Image Filter App") | |
# 3-column layout | |
col1, col2, col3 = st.columns([1, 1, 1]) # equal width | |
with col1: | |
st.header("π€ Upload Image") | |
st.caption("Max 5MB β’ Only PNG, JPG") | |
uploaded_file = st.file_uploader("Choose an image", type=["png", "jpg", "jpeg"]) | |
image = None | |
filtered_image = None | |
filter_option = "None" | |
if uploaded_file: | |
# Size check | |
uploaded_file.seek(0, io.SEEK_END) | |
file_size = uploaded_file.tell() | |
uploaded_file.seek(0) | |
if file_size > 5 * 1024 * 1024: | |
with col1: | |
st.error("β File too large! Must be under 5MB.") | |
else: | |
image = Image.open(uploaded_file) | |
with col2: | |
st.header("ποΈ Select Filter") | |
filter_option = st.selectbox("Choose a filter:", [ | |
"None", "Grayscale", "Invert Colors", "Sepia", "Blur", "Edge Detection", | |
"Posterize", "Emboss", "Solarize", "Brightness Boost", "Contrast Boost", | |
"Flip Horizontal", "Flip Vertical", "Rotate 90Β°" | |
]) | |
with col3: | |
st.header("π Preview Output") | |
if filter_option == "Grayscale": | |
filtered_image = ImageOps.grayscale(image) | |
st.image(filtered_image, caption="Grayscale", use_container_width=True) | |
elif filter_option == "Invert Colors": | |
if image.mode != "RGB": | |
image = image.convert("RGB") | |
filtered_image = ImageOps.invert(image) | |
st.image(filtered_image, caption="Inverted", use_container_width=True) | |
elif filter_option == "Sepia": | |
# Sepia filter | |
width, height = image.size | |
pixels = image.load() # create the pixel map | |
for py in range(height): | |
for px in range(width): | |
r, g, b = image.getpixel((px, py)) | |
tr = int(0.393 * r + 0.769 * g + 0.189 * b) | |
tg = int(0.349 * r + 0.686 * g + 0.168 * b) | |
tb = int(0.272 * r + 0.534 * g + 0.131 * b) | |
# Apply filter | |
if tr > 255: | |
tr = 255 | |
if tg > 255: | |
tg = 255 | |
if tb > 255: | |
tb = 255 | |
pixels[px, py] = (tr, tg, tb) | |
filtered_image = image | |
st.image(filtered_image, caption="Sepia", use_container_width=True) | |
elif filter_option == "Blur": | |
filtered_image = image.filter(ImageFilter.GaussianBlur(radius=5)) | |
st.image(filtered_image, caption="Blurred", use_container_width=True) | |
elif filter_option == "Edge Detection": | |
filtered_image = image.filter(ImageFilter.FIND_EDGES) | |
st.image(filtered_image, caption="Edge Detection", use_container_width=True) | |
elif filter_option == "Posterize": | |
filtered_image = ImageOps.posterize(image, 4) | |
st.image(filtered_image, caption="Posterized", use_container_width=True) | |
elif filter_option == "Emboss": | |
filtered_image = image.filter(ImageFilter.EMBOSS) | |
st.image(filtered_image, caption="Emboss", use_container_width=True) | |
elif filter_option == "Solarize": | |
filtered_image = ImageOps.solarize(image, threshold=128) | |
st.image(filtered_image, caption="Solarized", use_container_width=True) | |
elif filter_option == "Brightness Boost": | |
enhancer = ImageEnhance.Brightness(image) | |
filtered_image = enhancer.enhance(2) # increase brightness | |
st.image(filtered_image, caption="Brightness Boosted", use_container_width=True) | |
elif filter_option == "Contrast Boost": | |
enhancer = ImageEnhance.Contrast(image) | |
filtered_image = enhancer.enhance(2) # increase contrast | |
st.image(filtered_image, caption="Contrast Boosted", use_container_width=True) | |
elif filter_option == "Flip Horizontal": | |
filtered_image = image.transpose(Image.FLIP_LEFT_RIGHT) | |
st.image(filtered_image, caption="Flipped Horizontal", use_container_width=True) | |
elif filter_option == "Flip Vertical": | |
filtered_image = image.transpose(Image.FLIP_TOP_BOTTOM) | |
st.image(filtered_image, caption="Flipped Vertical", use_container_width=True) | |
elif filter_option == "Rotate 90Β°": | |
filtered_image = image.rotate(90) | |
st.image(filtered_image, caption="Rotated 90Β°", use_container_width=True) | |
else: | |
st.info("Select a filter to see output.") | |
with col1: | |
if filtered_image: | |
# Prepend "DEEZ_" to the original file name | |
file_name = uploaded_file.name | |
file_name_with_prefix = "DEEZ_" + file_name | |
# Allow downloading the filtered image with the modified name | |
st.download_button( | |
label="Download Filtered Image", | |
data=filtered_image.tobytes(), | |
file_name=file_name_with_prefix, | |
mime="image/png" | |
) | |