Spaces:
Sleeping
Sleeping
Commit
Β·
c3a39d7
1
Parent(s):
52b6f3f
app as updated
Browse files
app.py
CHANGED
|
@@ -2,86 +2,132 @@ import streamlit as st
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
| 26 |
-
elif filter_name == "Contrast":
|
| 27 |
-
alpha = scale / 50.0
|
| 28 |
-
return cv2.convertScaleAbs(image, alpha=alpha, beta=0)
|
| 29 |
-
elif filter_name == "Threshold":
|
| 30 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 31 |
-
_, thresh = cv2.threshold(gray, scale, 255, cv2.THRESH_BINARY)
|
| 32 |
-
return thresh
|
| 33 |
-
elif filter_name == "Sepia":
|
| 34 |
-
kernel = np.array([[0.272, 0.534, 0.131],
|
| 35 |
-
[0.349, 0.686, 0.168],
|
| 36 |
-
[0.393, 0.769, 0.189]])
|
| 37 |
-
return cv2.transform(image, kernel)
|
| 38 |
-
else:
|
| 39 |
-
return image
|
| 40 |
-
|
| 41 |
-
# Streamlit app
|
| 42 |
-
st.title("π¨ Image Processing App πΌοΈ")
|
| 43 |
-
st.write("Upload an image and apply filters to see the magic! β¨")
|
| 44 |
|
| 45 |
# Upload image
|
| 46 |
-
uploaded_file = st.file_uploader("
|
| 47 |
|
| 48 |
if uploaded_file is not None:
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
st.image(image, caption="Original Image", use_container_width=True)
|
| 54 |
-
|
| 55 |
-
# Select filter
|
| 56 |
-
filter_name = st.selectbox(
|
| 57 |
-
"Choose a filter ποΈ",
|
| 58 |
-
["Grayscale", "Blur", "Sharpen", "Edge Detection", "Brightness", "Contrast", "Threshold", "Sepia"]
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
# Add slider for filter scale
|
| 62 |
-
scale = st.slider(f"Adjust {filter_name} intensity βοΈ", 1, 100, 50)
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
processed_image = apply_filter(image, filter_name, scale)
|
| 66 |
-
|
| 67 |
-
# Display input and output images in the same row
|
| 68 |
col1, col2 = st.columns(2)
|
| 69 |
with col1:
|
| 70 |
-
st.
|
| 71 |
-
st.image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
with col2:
|
| 73 |
-
st.
|
| 74 |
-
st.image(
|
| 75 |
|
| 76 |
# Download button for processed image
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
st.download_button(
|
| 79 |
-
label="Download
|
| 80 |
-
data=
|
| 81 |
file_name="processed_image.png",
|
| 82 |
-
mime="image/png"
|
| 83 |
)
|
| 84 |
|
| 85 |
-
st.success("β
Image processing complete! Enjoy your masterpiece! π¨")
|
| 86 |
else:
|
| 87 |
-
st.info("
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
|
| 7 |
+
# App title and emojis
|
| 8 |
+
st.title("πΈ Image Processing App π¨")
|
| 9 |
+
st.write("Upload an image, apply filters, and download the processed image! β¨")
|
| 10 |
+
|
| 11 |
+
# Sidebar for feature selection
|
| 12 |
+
st.sidebar.header("π¨ Filter Options")
|
| 13 |
+
filter_type = st.sidebar.selectbox(
|
| 14 |
+
"Select a filter:",
|
| 15 |
+
[
|
| 16 |
+
"Blur",
|
| 17 |
+
"Edge Detection (Canny)",
|
| 18 |
+
"Grayscale",
|
| 19 |
+
"Sharpen",
|
| 20 |
+
"Sepia",
|
| 21 |
+
"Invert Colors",
|
| 22 |
+
"Brightness Adjustment",
|
| 23 |
+
"Contrast Adjustment",
|
| 24 |
+
],
|
| 25 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Upload image
|
| 28 |
+
uploaded_file = st.file_uploader(".Upload an image πΌοΈ", type=["jpg", "jpeg", "png"])
|
| 29 |
|
| 30 |
if uploaded_file is not None:
|
| 31 |
+
# Convert the file to an OpenCV image
|
| 32 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 33 |
+
img = cv2.imdecode(file_bytes, 1)
|
| 34 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert BGR to RGB
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Display original image
|
|
|
|
|
|
|
|
|
|
| 37 |
col1, col2 = st.columns(2)
|
| 38 |
with col1:
|
| 39 |
+
st.subheader("Original Image π·")
|
| 40 |
+
st.image(img, use_container_width=True)
|
| 41 |
+
|
| 42 |
+
# Apply filters based on user selection
|
| 43 |
+
processed_img = img.copy()
|
| 44 |
+
|
| 45 |
+
if filter_type == "Blur":
|
| 46 |
+
blur_type = st.sidebar.selectbox(
|
| 47 |
+
"Blur Type", ["Gaussian Blur", "Average Blur", "Median Blur", "Bilateral Filter"]
|
| 48 |
+
)
|
| 49 |
+
if blur_type == "Gaussian Blur":
|
| 50 |
+
kernel_size = st.sidebar.slider("Kernel Size π", 1, 31, 5, step=2)
|
| 51 |
+
sigma_x = st.sidebar.slider("Sigma X (Standard Deviation)", 0, 10, 0)
|
| 52 |
+
processed_img = cv2.GaussianBlur(processed_img, (kernel_size, kernel_size), sigma_x)
|
| 53 |
+
|
| 54 |
+
elif blur_type == "Average Blur":
|
| 55 |
+
kernel_size = st.sidebar.slider("Kernel Size π", 1, 31, 5, step=2)
|
| 56 |
+
processed_img = cv2.blur(processed_img, (kernel_size, kernel_size))
|
| 57 |
+
|
| 58 |
+
elif blur_type == "Median Blur":
|
| 59 |
+
kernel_size = st.sidebar.slider("Kernel Size π", 1, 31, 5, step=2)
|
| 60 |
+
processed_img = cv2.medianBlur(processed_img, kernel_size)
|
| 61 |
+
|
| 62 |
+
elif blur_type == "Bilateral Filter":
|
| 63 |
+
diameter = st.sidebar.slider("Diameter", 1, 31, 9, step=2)
|
| 64 |
+
sigma_color = st.sidebar.slider("Sigma Color", 1, 200, 75)
|
| 65 |
+
sigma_space = st.sidebar.slider("Sigma Space", 1, 200, 75)
|
| 66 |
+
processed_img = cv2.bilateralFilter(processed_img, diameter, sigma_color, sigma_space)
|
| 67 |
+
|
| 68 |
+
elif filter_type == "Edge Detection (Canny)":
|
| 69 |
+
lower_threshold = st.sidebar.slider("Lower Threshold β¬οΈ", 0, 255, 100)
|
| 70 |
+
upper_threshold = st.sidebar.slider("Upper Threshold β¬οΈ", 0, 255, 200)
|
| 71 |
+
processed_img = cv2.Canny(processed_img, lower_threshold, upper_threshold)
|
| 72 |
+
|
| 73 |
+
elif filter_type == "Grayscale":
|
| 74 |
+
grayscale_method = st.sidebar.selectbox(
|
| 75 |
+
"Grayscale Method", ["Weighted Average", "Simple Average"]
|
| 76 |
+
)
|
| 77 |
+
if grayscale_method == "Weighted Average":
|
| 78 |
+
processed_img = cv2.cvtColor(processed_img, cv2.COLOR_RGB2GRAY)
|
| 79 |
+
else:
|
| 80 |
+
processed_img = np.mean(processed_img, axis=2).astype(np.uint8)
|
| 81 |
+
|
| 82 |
+
elif filter_type == "Sharpen":
|
| 83 |
+
sharpness = st.sidebar.slider("Sharpness Intensity", 1.0, 10.0, 1.0)
|
| 84 |
+
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) * sharpness
|
| 85 |
+
processed_img = cv2.filter2D(processed_img, -1, kernel)
|
| 86 |
+
|
| 87 |
+
elif filter_type == "Sepia":
|
| 88 |
+
sepia_intensity = st.sidebar.slider("Sepia Intensity", 0.1, 1.0, 1.0)
|
| 89 |
+
sepia_filter = (
|
| 90 |
+
np.array(
|
| 91 |
+
[[0.272, 0.534, 0.131], [0.349, 0.686, 0.168], [0.393, 0.769, 0.189]]
|
| 92 |
+
)
|
| 93 |
+
* sepia_intensity
|
| 94 |
+
)
|
| 95 |
+
processed_img = cv2.transform(processed_img, sepia_filter)
|
| 96 |
+
processed_img = np.clip(processed_img, 0, 255)
|
| 97 |
+
|
| 98 |
+
elif filter_type == "Invert Colors":
|
| 99 |
+
blend_factor = st.sidebar.slider("Inversion Blend Factor", 0.0, 1.0, 1.0)
|
| 100 |
+
inverted_img = 255 - processed_img
|
| 101 |
+
processed_img = cv2.addWeighted(
|
| 102 |
+
processed_img, 1 - blend_factor, inverted_img, blend_factor, 0
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
elif filter_type == "Brightness Adjustment":
|
| 106 |
+
brightness = st.sidebar.slider("Brightness βοΈ", -100, 100, 0)
|
| 107 |
+
processed_img = cv2.convertScaleAbs(processed_img, alpha=1, beta=brightness)
|
| 108 |
+
|
| 109 |
+
elif filter_type == "Contrast Adjustment":
|
| 110 |
+
contrast = st.sidebar.slider("Contrast β‘", 0.1, 3.0, 1.0, step=0.1)
|
| 111 |
+
processed_img = cv2.convertScaleAbs(processed_img, alpha=contrast, beta=0)
|
| 112 |
+
|
| 113 |
+
# Display processed image
|
| 114 |
with col2:
|
| 115 |
+
st.subheader("Processed Image π―")
|
| 116 |
+
st.image(processed_img, use_container_width=True)
|
| 117 |
|
| 118 |
# Download button for processed image
|
| 119 |
+
st.markdown("---")
|
| 120 |
+
st.subheader("Download Processed Image πΎ")
|
| 121 |
+
buf = io.BytesIO()
|
| 122 |
+
pil_img = Image.fromarray(processed_img)
|
| 123 |
+
pil_img.save(buf, format="PNG")
|
| 124 |
+
byte_im = buf.getvalue()
|
| 125 |
st.download_button(
|
| 126 |
+
label="π₯ Download Image",
|
| 127 |
+
data=byte_im,
|
| 128 |
file_name="processed_image.png",
|
| 129 |
+
mime="image/png",
|
| 130 |
)
|
| 131 |
|
|
|
|
| 132 |
else:
|
| 133 |
+
st.info("Please upload an image to get started. π")
|