Spaces:
Build error
Build error
| import streamlit as st | |
| from PIL import Image, ImageEnhance, ImageFilter, ImageOps | |
| import numpy as np | |
| # Function to apply filters | |
| def apply_filter(image, filter_type): | |
| if filter_type == 'Grayscale': | |
| return image.convert('L') | |
| elif filter_type == 'Brightness': | |
| enhancer = ImageEnhance.Brightness(image) | |
| return enhancer.enhance(1.5) # Adjust brightness as per preference | |
| return image | |
| # Function to adjust brightness, contrast, saturation | |
| def adjust_brightness_contrast_saturation(image, brightness, contrast, saturation): | |
| enhancer_brightness = ImageEnhance.Brightness(image) | |
| image = enhancer_brightness.enhance(brightness) | |
| enhancer_contrast = ImageEnhance.Contrast(image) | |
| image = enhancer_contrast.enhance(contrast) | |
| enhancer_saturation = ImageEnhance.Color(image) | |
| image = enhancer_saturation.enhance(saturation) | |
| return image | |
| # Function to apply sharpness | |
| def adjust_sharpness(image, sharpness_factor): | |
| enhancer = ImageEnhance.Sharpness(image) | |
| return enhancer.enhance(sharpness_factor) | |
| # Function to resize image | |
| def resize_image(image, width, height): | |
| return image.resize((width, height)) | |
| # Function to rotate image | |
| def rotate_image(image, angle): | |
| return image.rotate(angle) | |
| # Function to flip image | |
| def flip_image(image, direction): | |
| if direction == 'Horizontal': | |
| return image.transpose(Image.FLIP_LEFT_RIGHT) | |
| elif direction == 'Vertical': | |
| return image.transpose(Image.FLIP_TOP_BOTTOM) | |
| # Main Streamlit interface | |
| def main(): | |
| st.title('Image Editor') | |
| uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"]) | |
| if uploaded_image is not None: | |
| image = Image.open(uploaded_image) | |
| st.image(image, caption="Uploaded Image", use_container_width=True) # Using container width for better fit | |
| # Filters and enhancements | |
| filter_type = st.selectbox("Select Filter", ['None', 'Grayscale', 'Brightness']) | |
| if filter_type != 'None': | |
| image = apply_filter(image, filter_type) | |
| st.image(image, caption="Filtered Image", use_container_width=True) | |
| # Image adjustments | |
| brightness = st.slider("Brightness", 0.0, 2.0, 1.0) | |
| contrast = st.slider("Contrast", 0.0, 2.0, 1.0) | |
| saturation = st.slider("Saturation", 0.0, 2.0, 1.0) | |
| image = adjust_brightness_contrast_saturation(image, brightness, contrast, saturation) | |
| # Sharpness adjustment | |
| sharpness_factor = st.slider("Sharpness", 0.0, 2.0, 1.0) | |
| image = adjust_sharpness(image, sharpness_factor) | |
| # Resize, rotate, and flip options | |
| width = st.slider("Resize Width", 50, 1000, image.width) | |
| height = st.slider("Resize Height", 50, 1000, image.height) | |
| image = resize_image(image, width, height) | |
| angle = st.slider("Rotation Angle", -180, 180, 0) | |
| image = rotate_image(image, angle) | |
| flip_direction = st.selectbox("Flip Direction", ['None', 'Horizontal', 'Vertical']) | |
| if flip_direction != 'None': | |
| image = flip_image(image, flip_direction) | |
| st.image(image, caption="Edited Image", use_container_width=True) | |
| # Save image with high quality (lossless or high-quality JPEG) | |
| if st.button("Download Image"): | |
| img_path = "edited_image.png" # Save as PNG for lossless quality | |
| image.save(img_path, format='PNG') # Save in PNG format for no quality loss | |
| with open(img_path, "rb") as file: | |
| st.download_button( | |
| label="Download Edited Image", | |
| data=file, | |
| file_name="edited_image.png", | |
| mime="image/png" | |
| ) | |
| if __name__ == "__main__": | |
| main() | |