imageGenerator / app.py
Ayesha003's picture
Update app.py
726ab2a verified
import streamlit as st
from PIL import Image, ImageEnhance, ImageFilter
# Custom CSS Styling
def apply_custom_css():
st.markdown("""
<style>
body {
background-color: #f4f4f4;
}
.main {
background-color: #ffffff;
padding: 20px;
border-radius: 10px;
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
}
.stButton > button {
background-color: #007BFF;
color: white;
border-radius: 5px;
padding: 10px 20px;
}
.stButton > button:hover {
background-color: #0056b3;
}
.stSidebar {
background-color: #f8f9fa;
}
</style>
""", unsafe_allow_html=True)
# Function to apply image filters
def apply_filters(image, grayscale, sepia, brightness, contrast, blur, flip, crop):
if grayscale:
image = image.convert("L")
if sepia:
sepia_filter = ImageEnhance.Color(image)
image = sepia_filter.enhance(1.5)
if brightness != 1.0:
enhancer = ImageEnhance.Brightness(image)
image = enhancer.enhance(brightness)
if contrast != 1.0:
enhancer = ImageEnhance.Contrast(image)
image = enhancer.enhance(contrast)
if blur > 0:
image = image.filter(ImageFilter.GaussianBlur(blur))
if flip == "Horizontal":
image = image.transpose(Image.FLIP_LEFT_RIGHT)
elif flip == "Vertical":
image = image.transpose(Image.FLIP_TOP_BOTTOM)
if crop:
left, top, right, bottom = crop
image = image.crop((left, top, right, bottom))
return image
# Main Application
def main():
apply_custom_css()
# Title and Banner
st.title("✨ Advanced Image Editor Pro")
st.markdown(
"Edit, Enhance, and Download Your Images Instantly with Professional Filters"
)
# Image Upload
uploaded_image = st.file_uploader("Upload an image to get started", type=["jpg", "jpeg", "png"])
if uploaded_image:
# Load the uploaded image
try:
image = Image.open(uploaded_image)
st.image(image, caption="Original Image", use_container_width=True)
except Exception as e:
st.error(f"Error loading image: {e}")
return
# Sidebar Filters
st.sidebar.header("🎨 Customize Your Image")
# Filter Options
grayscale = st.sidebar.checkbox("Apply Grayscale")
sepia = st.sidebar.checkbox("Apply Sepia")
brightness = st.sidebar.slider("Brightness πŸ”†", 0.5, 3.0, 1.0)
contrast = st.sidebar.slider("Contrast 🌟", 0.5, 3.0, 1.0)
blur = st.sidebar.slider("Blur Level 🌫️", 0, 10, 0)
flip = st.sidebar.radio("Flip Image πŸ”„", ["None", "Horizontal", "Vertical"])
# Crop Image
st.sidebar.header("Crop Image βœ‚οΈ")
left = st.sidebar.slider("Left", 0, image.width, 0)
top = st.sidebar.slider("Top", 0, image.height, 0)
right = st.sidebar.slider("Right", left + 10, image.width, image.width)
bottom = st.sidebar.slider("Bottom", top + 10, image.height, image.height)
crop = (left, top, right, bottom) if left < right and top < bottom else None
# Apply Filters
edited_image = apply_filters(image, grayscale, sepia, brightness, contrast, blur, flip, crop)
# Display Edited Image
st.subheader("Edited Image")
st.image(edited_image, caption="Your Enhanced Image", use_container_width=True)
# Download Section
st.sidebar.header("πŸ“₯ Download Your Image")
edited_image_path = "edited_image.png"
edited_image.save(edited_image_path)
with open(edited_image_path, "rb") as file:
st.sidebar.download_button(
label="Download Enhanced Image πŸ“‚",
data=file,
file_name="edited_image.png",
mime="image/png"
)
else:
# Message when no image is uploaded
st.info("πŸ‘† Upload an image to start editing.")
# Footer
st.markdown("---")
st.markdown("Designed with ❀️ using Streamlit. Happy Editing!")
# Run the app
if __name__ == "__main__":
main()