Image-Editor / app.py
Afeefa123's picture
Update app.py
88f47fc verified
import streamlit as st
from PIL import Image, ImageEnhance, ImageOps, ImageDraw
import io
# Set Page Config
st.set_page_config(page_title="Modern Image Editor", layout="centered")
# Custom Style
st.markdown(
"""
<style>
.stApp {
background-color: #f5f5f5;
font-family: Arial, sans-serif;
}
.title {
color: #333;
font-size: 32px;
font-weight: bold;
}
.caption {
color: #555;
}
</style>
""",
unsafe_allow_html=True
)
# Title
st.markdown("<div class='title'>Modern Image Editor</div>", unsafe_allow_html=True)
st.write("Upload an image, apply multiple filters, and download the edited image.")
# Upload image
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
if uploaded_file:
# Load the image
image = Image.open(uploaded_file)
st.image(image, caption="Original Image", use_container_width=True)
# Create a copy for editing
edited_image = image.copy()
# Sidebar for controls
st.sidebar.title("Editing Options")
# Grayscale filter
if st.sidebar.checkbox("Apply Grayscale"):
edited_image = ImageOps.grayscale(edited_image)
# Brightness adjustment
brightness = st.sidebar.slider("Adjust Brightness", 0.5, 3.0, 1.0, step=0.1)
enhancer = ImageEnhance.Brightness(edited_image)
edited_image = enhancer.enhance(brightness)
# Contrast adjustment
contrast = st.sidebar.slider("Adjust Contrast", 0.5, 3.0, 1.0, step=0.1)
enhancer = ImageEnhance.Contrast(edited_image)
edited_image = enhancer.enhance(contrast)
# Saturation adjustment
saturation = st.sidebar.slider("Adjust Saturation", 0.5, 3.0, 1.0, step=0.1)
enhancer = ImageEnhance.Color(edited_image)
edited_image = enhancer.enhance(saturation)
# Sharpness adjustment
sharpness = st.sidebar.slider("Adjust Sharpness", 0.5, 3.0, 1.0, step=0.1)
enhancer = ImageEnhance.Sharpness(edited_image)
edited_image = enhancer.enhance(sharpness)
# Add text overlay
if st.sidebar.checkbox("Add Text Overlay"):
text = st.sidebar.text_input("Enter Text", "Sample Text")
font_size = st.sidebar.slider("Font Size", 10, 100, 30)
draw = ImageDraw.Draw(edited_image)
draw.text((10, 10), text, fill="white")
# Flip options
flip_horizontal = st.sidebar.checkbox("Flip Horizontal")
if flip_horizontal:
edited_image = ImageOps.mirror(edited_image)
flip_vertical = st.sidebar.checkbox("Flip Vertical")
if flip_vertical:
edited_image = ImageOps.flip(edited_image)
# Rotate options
rotation = st.sidebar.slider("Rotate Image", 0, 360, 0, step=1)
if rotation:
edited_image = edited_image.rotate(rotation, expand=True)
# Border effect
if st.sidebar.checkbox("Add Border"):
border_width = st.sidebar.slider("Border Width", 1, 50, 10)
edited_image = ImageOps.expand(edited_image, border=border_width, fill="black")
# Display the edited image
st.image(edited_image, caption="Edited Image", use_container_width=True)
# Download the edited image
buf = io.BytesIO()
edited_image.save(buf, format="PNG")
byte_im = buf.getvalue()
st.download_button(
label="Download Edited Image",
data=byte_im,
file_name="edited_image.png",
mime="image/png"
)