Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,30 +3,30 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
def
|
| 7 |
# Convert to grayscale
|
| 8 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 9 |
|
| 10 |
-
# Apply median blur to
|
| 11 |
-
gray = cv2.medianBlur(gray,
|
| 12 |
|
| 13 |
-
#
|
| 14 |
edges = cv2.adaptiveThreshold(gray, 255,
|
| 15 |
cv2.ADAPTIVE_THRESH_MEAN_C,
|
| 16 |
cv2.THRESH_BINARY,
|
| 17 |
-
blockSize=
|
| 18 |
-
C=
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
pencil_effect = cv2.bitwise_and(
|
| 25 |
|
| 26 |
return pencil_effect
|
| 27 |
|
| 28 |
# Streamlit app layout
|
| 29 |
-
st.title("
|
| 30 |
st.subheader("Upload an image and adjust parameters")
|
| 31 |
|
| 32 |
# Upload image
|
|
@@ -40,14 +40,14 @@ if uploaded_file is not None:
|
|
| 40 |
# Create a container for sliders on the left side
|
| 41 |
with st.sidebar:
|
| 42 |
st.header("Adjust Parameters")
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
# Show a spinner while processing the pencil effect
|
| 48 |
with st.spinner('Processing...'):
|
| 49 |
-
# Apply the
|
| 50 |
-
pencil_image =
|
| 51 |
|
| 52 |
# Display original and pencil effect images side by side on the right side
|
| 53 |
col1, col2 = st.columns(2)
|
|
@@ -56,18 +56,18 @@ if uploaded_file is not None:
|
|
| 56 |
st.image(img.astype(np.uint8), caption='Original Image', use_column_width=True)
|
| 57 |
|
| 58 |
with col2:
|
| 59 |
-
st.image(pencil_image.astype(np.uint8), caption='
|
| 60 |
|
| 61 |
# Download button for the pencil effect image
|
| 62 |
if st.button("Download Pencil Effect Image"):
|
| 63 |
# Convert the pencil effect image to PIL format for download
|
| 64 |
pencil_image_pil = Image.fromarray(pencil_image)
|
| 65 |
-
pencil_image_pil.save("
|
| 66 |
|
| 67 |
-
with open("
|
| 68 |
st.download_button(
|
| 69 |
label="Download Image",
|
| 70 |
data=f,
|
| 71 |
-
file_name="
|
| 72 |
mime="image/png"
|
| 73 |
)
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
def artistic_pencil_effect(img, sigma_s=60, sigma_r=0.07, shade_factor=0.05):
|
| 7 |
# Convert to grayscale
|
| 8 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 9 |
|
| 10 |
+
# Apply a median blur to the grayscale image
|
| 11 |
+
gray = cv2.medianBlur(gray, 5)
|
| 12 |
|
| 13 |
+
# Use adaptive thresholding to create an edge mask
|
| 14 |
edges = cv2.adaptiveThreshold(gray, 255,
|
| 15 |
cv2.ADAPTIVE_THRESH_MEAN_C,
|
| 16 |
cv2.THRESH_BINARY,
|
| 17 |
+
blockSize=9,
|
| 18 |
+
C=2)
|
| 19 |
|
| 20 |
+
# Use cv2.stylization to create a cartoon-like effect
|
| 21 |
+
stylized = cv2.stylization(img, sigma_s=sigma_s, sigma_r=sigma_r)
|
| 22 |
|
| 23 |
+
# Use the edge mask to combine with the stylized image
|
| 24 |
+
pencil_effect = cv2.bitwise_and(stylized, stylized, mask=edges)
|
| 25 |
|
| 26 |
return pencil_effect
|
| 27 |
|
| 28 |
# Streamlit app layout
|
| 29 |
+
st.title("Artistic Pencil Effect Creator")
|
| 30 |
st.subheader("Upload an image and adjust parameters")
|
| 31 |
|
| 32 |
# Upload image
|
|
|
|
| 40 |
# Create a container for sliders on the left side
|
| 41 |
with st.sidebar:
|
| 42 |
st.header("Adjust Parameters")
|
| 43 |
+
sigma_s = st.slider("Sigma S (spatial sigma)", min_value=10, max_value=200, value=60)
|
| 44 |
+
sigma_r = st.slider("Sigma R (range sigma)", min_value=0.01, max_value=0.1, value=0.07, step=0.01)
|
| 45 |
+
shade_factor = st.slider("Shade Factor", min_value=0.01, max_value=0.1, value=0.05, step=0.01)
|
| 46 |
|
| 47 |
# Show a spinner while processing the pencil effect
|
| 48 |
with st.spinner('Processing...'):
|
| 49 |
+
# Apply the artistic pencil effect with selected parameters in real-time
|
| 50 |
+
pencil_image = artistic_pencil_effect(img, sigma_s, sigma_r, shade_factor)
|
| 51 |
|
| 52 |
# Display original and pencil effect images side by side on the right side
|
| 53 |
col1, col2 = st.columns(2)
|
|
|
|
| 56 |
st.image(img.astype(np.uint8), caption='Original Image', use_column_width=True)
|
| 57 |
|
| 58 |
with col2:
|
| 59 |
+
st.image(pencil_image.astype(np.uint8), caption='Artistic Pencil Effect', use_column_width=True)
|
| 60 |
|
| 61 |
# Download button for the pencil effect image
|
| 62 |
if st.button("Download Pencil Effect Image"):
|
| 63 |
# Convert the pencil effect image to PIL format for download
|
| 64 |
pencil_image_pil = Image.fromarray(pencil_image)
|
| 65 |
+
pencil_image_pil.save("artistic_pencil_effect_image.png")
|
| 66 |
|
| 67 |
+
with open("artistic_pencil_effect_image.png", "rb") as f:
|
| 68 |
st.download_button(
|
| 69 |
label="Download Image",
|
| 70 |
data=f,
|
| 71 |
+
file_name="artistic_pencil_effect_image.png",
|
| 72 |
mime="image/png"
|
| 73 |
)
|