Spaces:
Build error
Build error
File size: 2,461 Bytes
d16f691 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import streamlit as st
from PIL import Image, ImageEnhance, ImageFilter
import io
# Title and file uploader
st.title("Image Editor")
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
if uploaded_file:
# Load the uploaded image
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_column_width=True)
# Feature controls
st.sidebar.title("Editing Options")
# Brightness
brightness = st.sidebar.slider("Brightness", 0.1, 2.0, 1.0)
enhancer = ImageEnhance.Brightness(image)
image = enhancer.enhance(brightness)
# Contrast
contrast = st.sidebar.slider("Contrast", 0.1, 2.0, 1.0)
enhancer = ImageEnhance.Contrast(image)
image = enhancer.enhance(contrast)
# Sharpness
sharpness = st.sidebar.slider("Sharpness", 0.1, 2.0, 1.0)
enhancer = ImageEnhance.Sharpness(image)
image = enhancer.enhance(sharpness)
# Convert to grayscale
if st.sidebar.checkbox("Convert to Grayscale"):
image = image.convert("L")
# Apply filters
filter_option = st.sidebar.selectbox("Apply Filter", ["None", "BLUR", "CONTOUR", "DETAIL", "EDGE_ENHANCE", "SHARPEN"])
if filter_option == "BLUR":
image = image.filter(ImageFilter.BLUR)
elif filter_option == "CONTOUR":
image = image.filter(ImageFilter.CONTOUR)
elif filter_option == "DETAIL":
image = image.filter(ImageFilter.DETAIL)
elif filter_option == "EDGE_ENHANCE":
image = image.filter(ImageFilter.EDGE_ENHANCE)
elif filter_option == "SHARPEN":
image = image.filter(ImageFilter.SHARPEN)
# Crop
if st.sidebar.checkbox("Crop Image"):
left = st.sidebar.number_input("Left", 0, image.width, 0)
top = st.sidebar.number_input("Top", 0, image.height, 0)
right = st.sidebar.number_input("Right", left + 1, image.width, image.width)
bottom = st.sidebar.number_input("Bottom", top + 1, image.height, image.height)
image = image.crop((left, top, right, bottom))
# Display the edited image
st.image(image, caption="Edited Image", use_column_width=True)
# Download the edited image
buf = io.BytesIO()
image.save(buf, format="PNG")
byte_im = buf.getvalue()
st.download_button("Download Edited Image", data=byte_im, file_name="edited_image.png", mime="image/png")
# Instructions if no file uploaded
else:
st.write("Please upload an image to start editing.")
|