Besimplestudio commited on
Commit
d707b83
·
verified ·
1 Parent(s): bab2d4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -12,30 +12,30 @@ import gradio as gr
12
 
13
  # Function to convert image to sketch
14
  def convert_to_sketch(img, blur_strength, brightness, contrast):
15
- # Convert PIL Image to numpy array
16
  img = np.array(img)
17
-
18
- # Convert the image to grayscale
19
- img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # Use RGB2GRAY for PIL images
20
-
21
  # Invert the grayscale image
22
  img_inverted = cv2.bitwise_not(img_gray)
23
-
24
  # Apply Gaussian blur to the inverted image
25
  img_blur = cv2.GaussianBlur(img_inverted, (blur_strength, blur_strength), sigmaX=0, sigmaY=0)
26
-
27
  # Blend the grayscale and blurred inverted images to create the sketch effect
28
  img_blend = cv2.divide(img_gray, 255 - img_blur, scale=256)
29
-
30
  # Create a white background
31
  white_background = 255 * np.ones_like(img_blend)
32
-
33
  # Add the blended image to the white background
34
  sketch_with_bg = cv2.addWeighted(img_blend, 1, white_background, 1, 0)
35
-
36
  # Adjust brightness and contrast
37
  sketch_with_bg = adjust_brightness_contrast(sketch_with_bg, brightness, contrast)
38
-
39
  return sketch_with_bg
40
 
41
  # Function to adjust brightness and contrast
@@ -49,8 +49,18 @@ def sketch_interface(image, blur_strength, brightness, contrast):
49
  # Convert the input image to a sketch with adjustments
50
  sketch = convert_to_sketch(image, blur_strength, brightness, contrast)
51
 
 
 
 
 
 
52
  # Convert the processed numpy array back to a PIL Image
53
  output_image = Image.fromarray(sketch)
 
 
 
 
 
54
 
55
  # Return the processed sketch image
56
  return output_image
 
12
 
13
  # Function to convert image to sketch
14
  def convert_to_sketch(img, blur_strength, brightness, contrast):
15
+ # Convert PIL Image to numpy array (BGR format for OpenCV)
16
  img = np.array(img)
17
+
18
+ # Convert the image to grayscale (use RGB2GRAY for PIL images)
19
+ img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
20
+
21
  # Invert the grayscale image
22
  img_inverted = cv2.bitwise_not(img_gray)
23
+
24
  # Apply Gaussian blur to the inverted image
25
  img_blur = cv2.GaussianBlur(img_inverted, (blur_strength, blur_strength), sigmaX=0, sigmaY=0)
26
+
27
  # Blend the grayscale and blurred inverted images to create the sketch effect
28
  img_blend = cv2.divide(img_gray, 255 - img_blur, scale=256)
29
+
30
  # Create a white background
31
  white_background = 255 * np.ones_like(img_blend)
32
+
33
  # Add the blended image to the white background
34
  sketch_with_bg = cv2.addWeighted(img_blend, 1, white_background, 1, 0)
35
+
36
  # Adjust brightness and contrast
37
  sketch_with_bg = adjust_brightness_contrast(sketch_with_bg, brightness, contrast)
38
+
39
  return sketch_with_bg
40
 
41
  # Function to adjust brightness and contrast
 
49
  # Convert the input image to a sketch with adjustments
50
  sketch = convert_to_sketch(image, blur_strength, brightness, contrast)
51
 
52
+ # Check if the sketch image is non-empty
53
+ if sketch is None or sketch.size == 0:
54
+ print("Error: Sketch is empty!")
55
+ return None
56
+
57
  # Convert the processed numpy array back to a PIL Image
58
  output_image = Image.fromarray(sketch)
59
+
60
+ # Ensure the output image is valid
61
+ if output_image is None:
62
+ print("Error: Output image is None!")
63
+ return None
64
 
65
  # Return the processed sketch image
66
  return output_image