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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -29
app.py CHANGED
@@ -3,36 +3,30 @@ import cv2
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,34 +40,34 @@ if uploaded_file is not None:
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)
60
 
61
  with col1:
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
  )
 
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
  # 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)
54
 
55
  with col1:
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
  )