Image-Editor / app.py
muqeet1234's picture
Update app.py
bad1ff6 verified
import streamlit as st
from PIL import Image, ImageEnhance
import io
import requests
# Background Image URL (Corrected for direct download)
background_url = "https://drive.google.com/uc?id=1I9IbcLTS2FIsWfAa1A5XA-2NdrMIuy-t"
def set_background_from_url(url):
"""Set the background image of the app using a URL."""
response = requests.get(url)
if response.status_code == 200:
with open("background.jpg", "wb") as f:
f.write(response.content)
st.markdown(
"""
<style>
.stApp {
background-image: url('background.jpg');
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}
</style>
""",
unsafe_allow_html=True
)
else:
st.error("Unable to load the background image.")
# Set background image
set_background_from_url(background_url)
st.title("Image Editor")
# Upload an image
uploaded_image = st.file_uploader("Add Image", type=["jpg", "jpeg", "png"])
if uploaded_image:
image = Image.open(uploaded_image)
st.image(image, caption="Uploaded Image", use_column_width=True)
# Sidebar for filters
st.sidebar.header("Adjust Filters")
brightness = st.sidebar.slider("Brightness", 0.1, 3.0, 1.0)
contrast = st.sidebar.slider("Contrast", 0.1, 3.0, 1.0)
sharpness = st.sidebar.slider("Sharpness", 0.1, 3.0, 1.0)
# Resize options
st.sidebar.header("Resize Image")
width = st.sidebar.number_input("Width", value=image.width, step=1)
height = st.sidebar.number_input("Height", value=image.height, step=1)
# Apply Brightness
enhancer_brightness = ImageEnhance.Brightness(image)
image = enhancer_brightness.enhance(brightness)
# Apply Contrast
enhancer_contrast = ImageEnhance.Contrast(image)
image = enhancer_contrast.enhance(contrast)
# Apply Sharpness
enhancer_sharpness = ImageEnhance.Sharpness(image)
image = enhancer_sharpness.enhance(sharpness)
# Resize image
image = image.resize((int(width), int(height)))
# Display edited image
st.image(image, caption="Edited Image", use_column_width=True)
# Convert to bytes for download
img_bytes = io.BytesIO()
image.save(img_bytes, format="PNG")
img_bytes = img_bytes.getvalue()
# Download button
st.download_button("Download Image", data=img_bytes, file_name="edited_image.png", mime="image/png")