Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| from PIL import Image, ImageEnhance, ImageFilter | |
| import numpy as np | |
| import cv2 | |
| import io | |
| # Title | |
| st.title("Interactive Photo Editor - iPhone-like Features") | |
| # Sidebar: Upload image and edit controls | |
| st.sidebar.title("Edit Controls") | |
| uploaded_file = st.sidebar.file_uploader("Upload an Image", type=["png", "jpg", "jpeg"]) | |
| # Function to save image as a downloadable link | |
| def get_image_download_link(img: Image, filename: str): | |
| img.save(filename) | |
| with open(filename, "rb") as f: | |
| img_bytes = f.read() | |
| download_link = st.sidebar.download_button( | |
| label="Download Edited Image", | |
| data=img_bytes, | |
| file_name=filename, | |
| mime="image/jpeg" | |
| ) | |
| return download_link | |
| # Initialize image if uploaded | |
| if uploaded_file is not None: | |
| image = Image.open(uploaded_file) | |
| st.sidebar.image(image, caption="Uploaded Image", use_column_width=True) | |
| # Image Edit Options | |
| filter_option = st.sidebar.selectbox("Choose a Filter", ["None", "Vivid", "Warm", "Cool", "Black & White", "Sepia"]) | |
| brightness = st.sidebar.slider("Adjust Brightness", 0.0, 2.0, 1.0) | |
| contrast = st.sidebar.slider("Adjust Contrast", 0.0, 2.0, 1.0) | |
| saturation = st.sidebar.slider("Adjust Saturation", 0.0, 2.0, 1.0) | |
| # Apply chosen filters | |
| if filter_option == "Vivid": | |
| image = image.convert("RGB") | |
| np_img = np.array(image) | |
| np_img = cv2.cvtColor(np_img, cv2.COLOR_RGB2HSV) | |
| np_img[..., 1] = np_img[..., 1] * 1.5 # Boost saturation | |
| np_img = cv2.cvtColor(np_img, cv2.COLOR_HSV2RGB) | |
| image = Image.fromarray(np_img) | |
| elif filter_option == "Warm": | |
| image = image.convert("RGB") | |
| enhancer = ImageEnhance.Color(image) | |
| image = enhancer.enhance(1.2) | |
| elif filter_option == "Cool": | |
| image = image.convert("RGB") | |
| enhancer = ImageEnhance.Color(image) | |
| image = enhancer.enhance(0.8) | |
| elif filter_option == "Black & White": | |
| image = image.convert("L") | |
| elif filter_option == "Sepia": | |
| img_array = np.array(image) | |
| sepia_filter = np.array([[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]]) | |
| image = Image.fromarray(np.dot(img_array[...,:3], sepia_filter.T).clip(0, 255).astype(np.uint8)) | |
| # Apply manual adjustments | |
| 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 saturation != 1.0: | |
| enhancer = ImageEnhance.Color(image) | |
| image = enhancer.enhance(saturation) | |
| # Sidebar for cropping, resizing, rotating | |
| crop_enabled = st.sidebar.checkbox("Enable Crop", False) | |
| if crop_enabled: | |
| left = st.sidebar.slider("Left", 0, image.width, 0) | |
| top = st.sidebar.slider("Top", 0, image.height, 0) | |
| right = st.sidebar.slider("Right", left, image.width, image.width) | |
| bottom = st.sidebar.slider("Bottom", top, image.height, image.height) | |
| image = image.crop((left, top, right, bottom)) | |
| resize_enabled = st.sidebar.checkbox("Enable Resize", False) | |
| if resize_enabled: | |
| width = st.sidebar.slider("Width", 50, 1000, image.width) | |
| height = st.sidebar.slider("Height", 50, 1000, image.height) | |
| image = image.resize((width, height)) | |
| # Rotate/Flip Image | |
| rotate_enabled = st.sidebar.checkbox("Enable Rotation", False) | |
| if rotate_enabled: | |
| angle = st.sidebar.slider("Rotation Angle", 0, 360, 0) | |
| image = image.rotate(angle) | |
| flip_enabled = st.sidebar.checkbox("Flip Image", False) | |
| if flip_enabled: | |
| flip_option = st.sidebar.radio("Flip Option", ["Horizontal", "Vertical"]) | |
| if flip_option == "Horizontal": | |
| image = image.transpose(Image.FLIP_LEFT_RIGHT) | |
| else: | |
| image = image.transpose(Image.FLIP_TOP_BOTTOM) | |
| # Image preview in the main area | |
| st.image(image, caption="Edited Image", use_column_width=True) | |
| # Provide download option | |
| get_image_download_link(image, "edited_image.jpg") | |