Spaces:
Sleeping
Sleeping
| import os | |
| # Force installation of compatible versions | |
| os.system("pip install --force-reinstall numpy==1.23.5 opencv-python-headless==4.8.0.74") | |
| import cv2 | |
| import streamlit as st | |
| from PIL import Image | |
| import numpy as np | |
| import cv2 | |
| # Ensure OpenCV installation | |
| try: | |
| import cv2 | |
| except ModuleNotFoundError: | |
| os.system("pip install opencv-python-headless==4.8.0.74") | |
| import cv2 | |
| # Streamlit App Title | |
| st.title("Image Enhancer App") | |
| # File Uploader | |
| uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| # Load image using PIL and convert to OpenCV format | |
| image = Image.open(uploaded_file) | |
| image_np = np.array(image) | |
| # Display original image | |
| st.subheader("Original Image") | |
| st.image(image, caption="Uploaded Image", use_container_width=True) | |
| # Enhance Options | |
| st.subheader("Enhancement Options") | |
| enhancement = st.selectbox("Choose an enhancement type:", [ | |
| "Brightness Adjustment", | |
| "Contrast Enhancement", | |
| "Sharpening", | |
| "Denoising", | |
| "Edge Detection", | |
| "Apply All Enhancements" | |
| ]) | |
| enhanced_image = image_np | |
| if enhancement == "Brightness Adjustment": | |
| brightness = st.slider("Adjust Brightness", -100, 100, 0) | |
| enhanced_image = cv2.convertScaleAbs(enhanced_image, beta=brightness) | |
| elif enhancement == "Contrast Enhancement": | |
| contrast = st.slider("Adjust Contrast", 1.0, 3.0, 1.0) | |
| alpha = contrast # Simple contrast control | |
| beta = 0 # Simple brightness control | |
| enhanced_image = cv2.convertScaleAbs(enhanced_image, alpha=alpha, beta=beta) | |
| elif enhancement == "Sharpening": | |
| kernel = np.array([[0, -1, 0], | |
| [-1, 5, -1], | |
| [0, -1, 0]]) | |
| enhanced_image = cv2.filter2D(enhanced_image, -1, kernel) | |
| elif enhancement == "Denoising": | |
| h = st.slider("Denoising Strength", 1, 20, 10) | |
| enhanced_image = cv2.fastNlMeansDenoisingColored(enhanced_image, None, h, h, 7, 21) | |
| elif enhancement == "Edge Detection": | |
| threshold1 = st.slider("Threshold 1", 50, 200, 100) | |
| threshold2 = st.slider("Threshold 2", 50, 300, 200) | |
| gray_image = cv2.cvtColor(enhanced_image, cv2.COLOR_BGR2GRAY) | |
| edges = cv2.Canny(gray_image, threshold1, threshold2) | |
| enhanced_image = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR) | |
| elif enhancement == "Apply All Enhancements": | |
| # Apply all filters sequentially | |
| brightness = 50 | |
| contrast = 1.5 | |
| h = 10 | |
| kernel = np.array([[0, -1, 0], | |
| [-1, 5, -1], | |
| [0, -1, 0]]) | |
| gray_image = cv2.cvtColor(enhanced_image, cv2.COLOR_BGR2GRAY) | |
| edges = cv2.Canny(gray_image, 100, 200) | |
| enhanced_image = cv2.convertScaleAbs(enhanced_image, beta=brightness) | |
| enhanced_image = cv2.convertScaleAbs(enhanced_image, alpha=contrast, beta=0) | |
| enhanced_image = cv2.filter2D(enhanced_image, -1, kernel) | |
| enhanced_image = cv2.fastNlMeansDenoisingColored(enhanced_image, None, h, h, 7, 21) | |
| edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR) | |
| # Display Enhanced Image | |
| st.subheader("Enhanced Image") | |
| st.image(enhanced_image, caption="Enhanced Image", use_container_width=True) | |
| # Download Option | |
| st.subheader("Download Enhanced Image") | |
| result_image = Image.fromarray(enhanced_image) | |
| st.download_button( | |
| label="Download Image", | |
| data=result_image.tobytes(), | |
| file_name="enhanced_image.png", | |
| mime="image/png" | |
| ) | |