Besimplestudio commited on
Commit
d6f79e4
·
verified ·
1 Parent(s): 316e05c

Create Convert.py

Browse files
Files changed (1) hide show
  1. Convert.py +32 -0
Convert.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ from PIL import Image
3
+ import numpy as np
4
+ import gradio as gr
5
+
6
+ # Convert image to sketch function
7
+ def convert_to_sketch(img, blur_strength):
8
+ img = np.array(img) # Convert PIL Image to numpy array
9
+ img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
10
+ img_inverted = cv2.bitwise_not(img_gray)
11
+ img_blur = cv2.GaussianBlur(img_inverted, (blur_strength, blur_strength), sigmaX=0, sigmaY=0)
12
+ img_blend = cv2.divide(img_gray, 255 - img_blur, scale=256)
13
+ white_background = 255 * np.ones_like(img_blend)
14
+ sketch_with_bg = cv2.addWeighted(img_blend, 1, white_background, 1, 0)
15
+ return sketch_with_bg
16
+
17
+ # Gradio interface function
18
+ def sketch_interface(image, blur_strength):
19
+ sketch = convert_to_sketch(image, blur_strength)
20
+ return Image.fromarray(sketch)
21
+
22
+ # Create Gradio interface
23
+ interface = gr.Interface(
24
+ fn=sketch_interface,
25
+ inputs=[gr.Image(type="pil"), gr.Slider(1, 51, step=2, label="Blur Strength", value=21)],
26
+ outputs=gr.Image(type="pil"),
27
+ title="Cartoon to Sketch Converter",
28
+ description="Upload an image to convert it into a sketch, and adjust the blur strength for different effects."
29
+ )
30
+
31
+ # Launch the Gradio app
32
+ interface.launch()