File size: 1,193 Bytes
d6f79e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import cv2
from PIL import Image
import numpy as np
import gradio as gr

# Convert image to sketch function
def convert_to_sketch(img, blur_strength):
    img = np.array(img)  # Convert PIL Image to numpy array
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_inverted = cv2.bitwise_not(img_gray)
    img_blur = cv2.GaussianBlur(img_inverted, (blur_strength, blur_strength), sigmaX=0, sigmaY=0)
    img_blend = cv2.divide(img_gray, 255 - img_blur, scale=256)
    white_background = 255 * np.ones_like(img_blend)
    sketch_with_bg = cv2.addWeighted(img_blend, 1, white_background, 1, 0)
    return sketch_with_bg

# Gradio interface function
def sketch_interface(image, blur_strength):
    sketch = convert_to_sketch(image, blur_strength)
    return Image.fromarray(sketch)

# Create Gradio interface
interface = gr.Interface(
    fn=sketch_interface,
    inputs=[gr.Image(type="pil"), gr.Slider(1, 51, step=2, label="Blur Strength", value=21)],
    outputs=gr.Image(type="pil"),
    title="Cartoon to Sketch Converter",
    description="Upload an image to convert it into a sketch, and adjust the blur strength for different effects."
)

# Launch the Gradio app
interface.launch()