Spaces:
Sleeping
Sleeping
File size: 3,619 Bytes
1ad8157 5472b30 fc8c35a 5472b30 482fc99 1ad8157 482fc99 d4023fa e9ed3ed 1ad8157 4750274 482fc99 1ad8157 482fc99 1ad8157 482fc99 1ad8157 4750274 1ad8157 dd7bca6 d4023fa 1ad8157 dd7bca6 1ad8157 d4023fa dd7bca6 1ad8157 dd7bca6 1ad8157 dd7bca6 1ad8157 dd7bca6 1ad8157 dd7bca6 1ad8157 dd7bca6 1ad8157 dd7bca6 4750274 1ad8157 dd7bca6 4750274 1ad8157 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | 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"
)
|