Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,46 +3,36 @@ 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
|
| 11 |
-
|
| 12 |
|
| 13 |
# Adaptive thresholding for edges
|
| 14 |
-
edges = cv2.adaptiveThreshold(
|
| 15 |
cv2.ADAPTIVE_THRESH_MEAN_C,
|
| 16 |
cv2.THRESH_BINARY,
|
| 17 |
blockSize=block_size,
|
| 18 |
C=c_value)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
color = cv2.bilateralFilter(img, d=d_value, sigmaColor=sigma_color, sigmaSpace=sigma_space)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Reduce colors by applying a threshold on the grayscale image
|
| 27 |
-
_, color_thresh = cv2.threshold(color_gray, 200, 255, cv2.THRESH_BINARY_INV)
|
| 28 |
-
|
| 29 |
-
# Create a sketch effect by blending edges with the smoothed color image
|
| 30 |
-
sketch = cv2.bitwise_and(color, color, mask=edges)
|
| 31 |
-
|
| 32 |
-
# Ensure both images are of the same size before blending
|
| 33 |
-
if sketch.shape[:2] != img.shape[:2]:
|
| 34 |
-
sketch = cv2.resize(sketch, (img.shape[1], img.shape[0]))
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
cartoon = cv2.
|
| 41 |
|
| 42 |
return cartoon
|
| 43 |
|
| 44 |
# Streamlit app layout
|
| 45 |
-
st.title("
|
| 46 |
st.subheader("Upload an image and adjust parameters")
|
| 47 |
|
| 48 |
# Upload image
|
|
@@ -56,17 +46,14 @@ if uploaded_file is not None:
|
|
| 56 |
# Create a container for sliders on the left side
|
| 57 |
with st.sidebar:
|
| 58 |
st.header("Adjust Parameters")
|
| 59 |
-
|
| 60 |
block_size = st.slider("Block Size", min_value=3, max_value=21, value=9, step=2)
|
| 61 |
c_value = st.slider("C Value", min_value=0, max_value=20, value=5) # Increased default value for more effect
|
| 62 |
-
d_value = st.slider("Diameter (d)", min_value=1, max_value=15, value=9)
|
| 63 |
-
sigma_color = st.slider("Sigma Color", min_value=0, max_value=400, value=100) # Reduced for more sketch effect
|
| 64 |
-
sigma_space = st.slider("Sigma Space", min_value=0, max_value=400, value=100) # Reduced for more sketch effect
|
| 65 |
|
| 66 |
# Show a spinner while processing the cartoonization
|
| 67 |
with st.spinner('Processing...'):
|
| 68 |
# Cartoonize the image with selected parameters in real-time
|
| 69 |
-
cartoon_image = cartoonize_image(img,
|
| 70 |
|
| 71 |
# Display original and cartoonized images side by side on the right side
|
| 72 |
col1, col2 = st.columns(2)
|
|
@@ -75,18 +62,18 @@ if uploaded_file is not None:
|
|
| 75 |
st.image(img.astype(np.uint8), caption='Original Image', use_column_width=True)
|
| 76 |
|
| 77 |
with col2:
|
| 78 |
-
st.image(cartoon_image.astype(np.uint8), caption='Cartoonized Image', use_column_width=True)
|
| 79 |
|
| 80 |
# Download button for cartoonized image
|
| 81 |
if st.button("Download Cartoonized Image"):
|
| 82 |
# Convert the cartoonized image to PIL format for download
|
| 83 |
cartoon_image_pil = Image.fromarray(cartoon_image)
|
| 84 |
-
cartoon_image_pil.save("
|
| 85 |
|
| 86 |
-
with open("
|
| 87 |
st.download_button(
|
| 88 |
label="Download Image",
|
| 89 |
data=f,
|
| 90 |
-
file_name="
|
| 91 |
mime="image/png"
|
| 92 |
)
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
def pencil_effect(img, high_blur_value, block_size, c_value):
|
| 7 |
# Convert to grayscale
|
| 8 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 9 |
|
| 10 |
+
# Apply high Gaussian blur
|
| 11 |
+
blurred = cv2.GaussianBlur(gray, (high_blur_value, high_blur_value), 0)
|
| 12 |
|
| 13 |
# Adaptive thresholding for edges
|
| 14 |
+
edges = cv2.adaptiveThreshold(blurred, 255,
|
| 15 |
cv2.ADAPTIVE_THRESH_MEAN_C,
|
| 16 |
cv2.THRESH_BINARY,
|
| 17 |
blockSize=block_size,
|
| 18 |
C=c_value)
|
| 19 |
|
| 20 |
+
return edges
|
|
|
|
| 21 |
|
| 22 |
+
def cartoonize_image(img, high_blur_value, block_size, c_value):
|
| 23 |
+
# Apply pencil effect to get edges
|
| 24 |
+
edges = pencil_effect(img, high_blur_value, block_size, c_value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Reduce colors using bilateral filter
|
| 27 |
+
color = cv2.bilateralFilter(img, d=9, sigmaColor=100, sigmaSpace=100)
|
| 28 |
|
| 29 |
+
# Combine edges with color image to create a more artistic effect
|
| 30 |
+
cartoon = cv2.bitwise_and(color, color, mask=edges)
|
| 31 |
|
| 32 |
return cartoon
|
| 33 |
|
| 34 |
# Streamlit app layout
|
| 35 |
+
st.title("Pencil Effect Cartoonizer")
|
| 36 |
st.subheader("Upload an image and adjust parameters")
|
| 37 |
|
| 38 |
# Upload image
|
|
|
|
| 46 |
# Create a container for sliders on the left side
|
| 47 |
with st.sidebar:
|
| 48 |
st.header("Adjust Parameters")
|
| 49 |
+
high_blur_value = st.slider("High Blur Value", min_value=3, max_value=31, value=21, step=2) # Increased range for more blur
|
| 50 |
block_size = st.slider("Block Size", min_value=3, max_value=21, value=9, step=2)
|
| 51 |
c_value = st.slider("C Value", min_value=0, max_value=20, value=5) # Increased default value for more effect
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# Show a spinner while processing the cartoonization
|
| 54 |
with st.spinner('Processing...'):
|
| 55 |
# Cartoonize the image with selected parameters in real-time
|
| 56 |
+
cartoon_image = cartoonize_image(img, high_blur_value, block_size, c_value)
|
| 57 |
|
| 58 |
# Display original and cartoonized images side by side on the right side
|
| 59 |
col1, col2 = st.columns(2)
|
|
|
|
| 62 |
st.image(img.astype(np.uint8), caption='Original Image', use_column_width=True)
|
| 63 |
|
| 64 |
with col2:
|
| 65 |
+
st.image(cartoon_image.astype(np.uint8), caption='Pencil Effect Cartoonized Image', use_column_width=True)
|
| 66 |
|
| 67 |
# Download button for cartoonized image
|
| 68 |
if st.button("Download Cartoonized Image"):
|
| 69 |
# Convert the cartoonized image to PIL format for download
|
| 70 |
cartoon_image_pil = Image.fromarray(cartoon_image)
|
| 71 |
+
cartoon_image_pil.save("pencil_cartoonized_image.png")
|
| 72 |
|
| 73 |
+
with open("pencil_cartoonized_image.png", "rb") as f:
|
| 74 |
st.download_button(
|
| 75 |
label="Download Image",
|
| 76 |
data=f,
|
| 77 |
+
file_name="pencil_cartoonized_image.png",
|
| 78 |
mime="image/png"
|
| 79 |
)
|