import streamlit as st from PIL import Image, ImageEnhance, ImageFilter import numpy as np # Title of the app st.title("Image Enhancer and Filter Application") # File uploader to upload the image uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) if uploaded_file is not None: # Open the image image = Image.open(uploaded_file) # Display the original image st.image(image, caption='Original Image', use_container_width=True) # Sidebar options for filters st.sidebar.header("Select Filter") filter_option = st.sidebar.selectbox("Choose a filter:", ["None", "Enhance", "Grayscale", "Colorize", "Blur", "Sharpen", "Edge Enhance"]) # Enhancement options if filter_option == "Enhance": st.sidebar.header("Adjust Brightness and Contrast") brightness = st.sidebar.slider("Brightness", 0.0, 2.0, 1.0) contrast = st.sidebar.slider("Contrast", 0.0, 2.0, 1.0) # Enhance brightness and contrast enhancer_brightness = ImageEnhance.Brightness(image) enhanced_image_brightness = enhancer_brightness.enhance(brightness) enhancer_contrast = ImageEnhance.Contrast(enhanced_image_brightness) enhanced_image = enhancer_contrast.enhance(contrast) st.image(enhanced_image, caption='Enhanced Image', use_container_width=True) elif filter_option == "Grayscale": # Convert to grayscale gray_image = image.convert("L") st.image(gray_image, caption='Grayscale Image', use_container_width=True) elif filter_option == "Colorize": # Ensure the uploaded image is in grayscale for colorization if image.convert('L') == image: colorizer = DeOldify() colorized_image = colorizer.colorize(image) st.image(colorized_image, caption='Colorized Image', use_container_width=True) else: st.sidebar.warning("Please upload a black and white image for colorization.") elif filter_option == "Blur": # Apply a blur filter blurred_image = image.filter(ImageFilter.BLUR) st.image(blurred_image, caption='Blurred Image', use_container_width=True) elif filter_option == "Sharpen": # Apply a sharpen filter sharpened_image = image.filter(ImageFilter.SHARPEN) st.image(sharpened_image, caption='Sharpened Image', use_container_width=True) elif filter_option == "Edge Enhance": # Apply an edge enhancement filter edge_enhanced_image = image.filter(ImageFilter.EDGE_ENHANCE) st.image(edge_enhanced_image, caption='Edge Enhanced Image', use_container_width=True) # Option to download the processed image processed_image = enhanced_image if filter_option == "Enhance" else gray_image if filter_option == "Grayscale" else colorized_image if filter_option == "Colorize" else blurred_image if filter_option == "Blur" else sharpened_image if filter_option == "Sharpen" else edge_enhanced_image if filter_option == "Edge Enhance" else None if processed_image: st.sidebar.download_button("Download Processed Image", data=np.array(processed_image).tobytes(), file_name="processed_image.png", mime="image/png")