pvyas96 commited on
Commit
e498f6e
·
verified ·
1 Parent(s): 0f13a4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -20
app.py CHANGED
@@ -3,30 +3,30 @@ import cv2
3
  import numpy as np
4
  from PIL import Image
5
 
6
- def color_pencil_effect(img, median_blur_value, block_size, c_value):
7
  # Convert to grayscale
8
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
9
 
10
- # Apply median blur to smooth the grayscale image
11
- gray = cv2.medianBlur(gray, median_blur_value)
12
 
13
- # Adaptive thresholding for edges
14
  edges = cv2.adaptiveThreshold(gray, 255,
15
  cv2.ADAPTIVE_THRESH_MEAN_C,
16
  cv2.THRESH_BINARY,
17
- blockSize=block_size,
18
- C=c_value)
19
 
20
- # Reduce colors using bilateral filter to achieve a pencil effect
21
- color = cv2.bilateralFilter(img, d=9, sigmaColor=150, sigmaSpace=150)
22
 
23
- # Combine edges with the color image to create the pencil effect
24
- pencil_effect = cv2.bitwise_and(color, color, mask=edges)
25
 
26
  return pencil_effect
27
 
28
  # Streamlit app layout
29
- st.title("Color Pencil Effect Creator")
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
- median_blur_value = st.slider("Median Blur Value", min_value=3, max_value=11, value=5, step=2)
44
- block_size = st.slider("Block Size", min_value=3, max_value=21, value=9, step=2)
45
- c_value = st.slider("C Value", min_value=0, max_value=20, value=5) # Increased default value for more effect
46
 
47
  # Show a spinner while processing the pencil effect
48
  with st.spinner('Processing...'):
49
- # Apply the color pencil effect with selected parameters in real-time
50
- pencil_image = color_pencil_effect(img, median_blur_value, block_size, c_value)
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='Color 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("color_pencil_effect_image.png")
66
 
67
- with open("color_pencil_effect_image.png", "rb") as f:
68
  st.download_button(
69
  label="Download Image",
70
  data=f,
71
- file_name="color_pencil_effect_image.png",
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
  )