imageeditor / app.py
drwaseem's picture
Update app.py
b4d0d89 verified
import streamlit as st
from PIL import Image, ImageEnhance, ImageFilter
import io
# Function to apply grayscale filter
def apply_grayscale(img):
return img.convert("L")
# Function to adjust brightness
def adjust_brightness(img, factor):
enhancer = ImageEnhance.Brightness(img)
return enhancer.enhance(factor)
# Function to adjust contrast
def adjust_contrast(img, factor):
enhancer = ImageEnhance.Contrast(img)
return enhancer.enhance(factor)
# Function to adjust sharpness
def adjust_sharpness(img, factor):
enhancer = ImageEnhance.Sharpness(img)
return enhancer.enhance(factor)
# Function to rotate image
def rotate_image(img, angle):
return img.rotate(angle)
# Function to resize image
def resize_image(img, width, height):
return img.resize((width, height))
# Function to apply a blur filter
def apply_blur(img):
return img.filter(ImageFilter.BLUR)
# Streamlit Interface
def main():
st.title("Advanced Image Editor")
# Upload image
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Open the image
img = Image.open(uploaded_file)
# Display the uploaded image
st.image(img, caption="Uploaded Image", use_column_width=True)
# Apply Grayscale Filter
if st.checkbox("Apply Grayscale"):
img = apply_grayscale(img)
st.image(img, caption="Grayscale Image", use_column_width=True)
# Brightness Adjustment
brightness_factor = st.slider("Adjust Brightness", 0.5, 2.0, 1.0)
img = adjust_brightness(img, brightness_factor)
st.image(img, caption=f"Brightness Adjusted (Factor: {brightness_factor})", use_column_width=True)
# Contrast Adjustment
contrast_factor = st.slider("Adjust Contrast", 0.5, 2.0, 1.0)
img = adjust_contrast(img, contrast_factor)
st.image(img, caption=f"Contrast Adjusted (Factor: {contrast_factor})", use_column_width=True)
# Sharpness Adjustment
sharpness_factor = st.slider("Adjust Sharpness", 0.5, 2.0, 1.0)
img = adjust_sharpness(img, sharpness_factor)
st.image(img, caption=f"Sharpness Adjusted (Factor: {sharpness_factor})", use_column_width=True)
# Rotate Image
rotation_angle = st.slider("Rotate Image", 0, 360, 0)
if rotation_angle != 0:
img = rotate_image(img, rotation_angle)
st.image(img, caption=f"Rotated Image (Angle: {rotation_angle}°)", use_column_width=True)
# Resize Image
new_width = st.slider("Resize Image - Width", 100, 2000, img.width)
new_height = st.slider("Resize Image - Height", 100, 2000, img.height)
if new_width != img.width or new_height != img.height:
img = resize_image(img, new_width, new_height)
st.image(img, caption=f"Resized Image ({new_width}x{new_height})", use_column_width=True)
# Apply Blur Filter
if st.checkbox("Apply Blur"):
img = apply_blur(img)
st.image(img, caption="Blurred Image", use_column_width=True)
# Download Button
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG')
img_byte_arr.seek(0)
st.download_button(
label="Download Edited Image",
data=img_byte_arr,
file_name="edited_image.png",
mime="image/png"
)
# Run the app
if __name__ == "__main__":
main()