Spaces:
Build error
Build error
| import streamlit as st | |
| from PIL import Image, ImageEnhance, ImageFilter | |
| import numpy as np | |
| import cv2 | |
| import io | |
| # Function to adjust brightness, contrast, saturation | |
| def adjust_image(img, brightness=1.0, contrast=1.0, saturation=1.0): | |
| enhancer = ImageEnhance.Brightness(img) | |
| img = enhancer.enhance(brightness) | |
| enhancer = ImageEnhance.Contrast(img) | |
| img = enhancer.enhance(contrast) | |
| enhancer = ImageEnhance.Color(img) | |
| img = enhancer.enhance(saturation) | |
| return img | |
| # Function to apply grayscale filter | |
| def apply_grayscale(img): | |
| return img.convert("L") | |
| # Function to apply blur effect | |
| def apply_blur(img): | |
| return img.filter(ImageFilter.GaussianBlur(radius=5)) | |
| # Function to crop image | |
| def crop_image(img, left, upper, right, lower): | |
| return img.crop((left, upper, right, lower)) | |
| # Function to cut a piece of the image | |
| def cut_piece(img, x, y, width, height): | |
| img_np = np.array(img) | |
| cut_img = img_np[y:y+height, x:x+width] | |
| return Image.fromarray(cut_img) | |
| # Streamlit App UI | |
| st.title("Image Editing App") | |
| uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"]) | |
| if uploaded_file is not None: | |
| # Open the uploaded image | |
| img = Image.open(uploaded_file) | |
| st.image(img, caption="Uploaded Image", use_column_width=True) | |
| # Adjust brightness, contrast, and saturation | |
| st.sidebar.header("Adjust Image") | |
| brightness = st.sidebar.slider("Brightness", 0.0, 2.0, 1.0) | |
| contrast = st.sidebar.slider("Contrast", 0.0, 2.0, 1.0) | |
| saturation = st.sidebar.slider("Saturation", 0.0, 2.0, 1.0) | |
| img = adjust_image(img, brightness, contrast, saturation) | |
| # Filters and effects | |
| filter_option = st.sidebar.selectbox( | |
| "Select a Filter", | |
| ["None", "Grayscale", "Blur"] | |
| ) | |
| if filter_option == "Grayscale": | |
| img = apply_grayscale(img) | |
| elif filter_option == "Blur": | |
| img = apply_blur(img) | |
| # Crop and cut image functionality | |
| crop_option = st.sidebar.checkbox("Crop Image") | |
| if crop_option: | |
| left = st.sidebar.slider("Left", 0, img.width, 0) | |
| upper = st.sidebar.slider("Upper", 0, img.height, 0) | |
| right = st.sidebar.slider("Right", left, img.width, img.width) | |
| lower = st.sidebar.slider("Lower", upper, img.height, img.height) | |
| img = crop_image(img, left, upper, right, lower) | |
| cut_option = st.sidebar.checkbox("Cut a Piece of Image") | |
| if cut_option: | |
| x = st.sidebar.slider("X position", 0, img.width, 0) | |
| y = st.sidebar.slider("Y position", 0, img.height, 0) | |
| width = st.sidebar.slider("Width", 1, img.width - x, 100) | |
| height = st.sidebar.slider("Height", 1, img.height - y, 100) | |
| img = cut_piece(img, x, y, width, height) | |
| # Display edited image | |
| st.image(img, caption="Edited Image", use_column_width=True) | |
| # Option to download edited image | |
| buffered = io.BytesIO() | |
| img.save(buffered, format="PNG") | |
| st.download_button(label="Download Image", data=buffered.getvalue(), file_name="edited_image.png", mime="image/png") | |