pvyas96 commited on
Commit
aff8b10
·
verified ·
1 Parent(s): 5644677

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -10
app.py CHANGED
@@ -6,7 +6,7 @@ from PIL import Image
6
  def cartoonize_image(img, median_blur_value, block_size, c_value, d_value, sigma_color, sigma_space):
7
  # Convert to grayscale
8
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
9
-
10
  # Apply median blur
11
  gray = cv2.medianBlur(gray, median_blur_value)
12
 
@@ -20,19 +20,15 @@ def cartoonize_image(img, median_blur_value, block_size, c_value, d_value, sigma
20
  # Bilateral filter for color smoothing
21
  color = cv2.bilateralFilter(img, d=d_value, sigmaColor=sigma_color, sigmaSpace=sigma_space)
22
 
23
- # Convert color image to grayscale and then back to BGR to reduce colors
24
- color_gray = cv2.cvtColor(color, cv2.COLOR_BGR2GRAY)
25
- color_gray = cv2.medianBlur(color_gray, 5)
26
-
27
  # Create a sketch effect by blending edges with the smoothed color image
28
- sketch = cv2.bitwise_and(color_gray, color_gray, mask=edges)
29
 
30
  # Ensure both images are of the same size before blending
31
- if color.shape != sketch.shape:
32
  sketch = cv2.resize(sketch, (color.shape[1], color.shape[0]))
33
 
34
  # Combine the sketch with the original color image to create a more artistic effect
35
- cartoon = cv2.addWeighted(color, 0.5, sketch[:, :, np.newaxis], 0.5, 0) # Ensure sketch has 3 channels
36
 
37
  return cartoon
38
 
@@ -67,10 +63,10 @@ if uploaded_file is not None:
67
  col1, col2 = st.columns(2)
68
 
69
  with col1:
70
- st.image(img, caption='Original Image', use_column_width=True)
71
 
72
  with col2:
73
- st.image(cartoon_image, caption='Cartoonized Image', use_column_width=True)
74
 
75
  # Download button for cartoonized image
76
  if st.button("Download Cartoonized Image"):
 
6
  def cartoonize_image(img, median_blur_value, block_size, c_value, d_value, sigma_color, sigma_space):
7
  # Convert to grayscale
8
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
9
+
10
  # Apply median blur
11
  gray = cv2.medianBlur(gray, median_blur_value)
12
 
 
20
  # Bilateral filter for color smoothing
21
  color = cv2.bilateralFilter(img, d=d_value, sigmaColor=sigma_color, sigmaSpace=sigma_space)
22
 
 
 
 
 
23
  # Create a sketch effect by blending edges with the smoothed color image
24
+ sketch = cv2.bitwise_and(color, color, mask=edges)
25
 
26
  # Ensure both images are of the same size before blending
27
+ if color.shape[:2] != sketch.shape[:2]:
28
  sketch = cv2.resize(sketch, (color.shape[1], color.shape[0]))
29
 
30
  # Combine the sketch with the original color image to create a more artistic effect
31
+ cartoon = cv2.addWeighted(color, 0.5, sketch, 0.5, 0)
32
 
33
  return cartoon
34
 
 
63
  col1, col2 = st.columns(2)
64
 
65
  with col1:
66
+ st.image(img.astype(np.uint8), caption='Original Image', use_column_width=True)
67
 
68
  with col2:
69
+ st.image(cartoon_image.astype(np.uint8), caption='Cartoonized Image', use_column_width=True)
70
 
71
  # Download button for cartoonized image
72
  if st.button("Download Cartoonized Image"):