File size: 2,895 Bytes
f45aefd
50e7fdf
 
 
 
 
 
 
f45aefd
d835849
 
 
f45aefd
1e24872
d707b83
1e24872
d707b83
 
 
 
1e24872
d835849
d707b83
1e24872
d835849
d707b83
1e24872
d835849
d707b83
1e24872
d835849
d707b83
1e24872
d835849
d707b83
1e24872
 
d707b83
d835849
 
1e24872
 
50e7fdf
1e24872
 
 
d835849
1e24872
 
 
f45aefd
d707b83
 
 
 
 
bab2d4f
2541aba
d707b83
 
 
 
 
f45aefd
5312728
0db9a41
d835849
 
 
 
6c92a72
5312728
1e24872
 
 
6c92a72
f45aefd
d835849
5312728
d835849
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Attempt to import cv2 and install opencv-python-headless if not available
try:
    import cv2
except ImportError:
    import subprocess
    subprocess.check_call(["pip", "install", "opencv-python-headless"])
    import cv2

from PIL import Image
import numpy as np
import gradio as gr

# Function to convert image to sketch
def convert_to_sketch(img, blur_strength, brightness, contrast):
    # Convert PIL Image to numpy array (BGR format for OpenCV)
    img = np.array(img)
    
    # Convert the image to grayscale (use RGB2GRAY for PIL images)
    img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    
    # Invert the grayscale image
    img_inverted = cv2.bitwise_not(img_gray)
    
    # Apply Gaussian blur to the inverted image
    img_blur = cv2.GaussianBlur(img_inverted, (blur_strength, blur_strength), sigmaX=0, sigmaY=0)
    
    # Blend the grayscale and blurred inverted images to create the sketch effect
    img_blend = cv2.divide(img_gray, 255 - img_blur, scale=256)
    
    # Create a white background
    white_background = 255 * np.ones_like(img_blend)
    
    # Add the blended image to the white background
    sketch_with_bg = cv2.addWeighted(img_blend, 1, white_background, 1, 0)
    
    # Adjust brightness and contrast
    sketch_with_bg = adjust_brightness_contrast(sketch_with_bg, brightness, contrast)
    
    return sketch_with_bg

# Function to adjust brightness and contrast
def adjust_brightness_contrast(img, brightness, contrast):
    # Apply contrast adjustment
    img = cv2.convertScaleAbs(img, alpha=contrast, beta=brightness)
    return img

# Gradio interface function
def sketch_interface(image, blur_strength, brightness, contrast):
    # Convert the input image to a sketch with adjustments
    sketch = convert_to_sketch(image, blur_strength, brightness, contrast)

    # Check if the sketch image is non-empty
    if sketch is None or sketch.size == 0:
        print("Error: Sketch is empty!")
        return None

    # Convert the processed numpy array back to a PIL Image
    output_image = Image.fromarray(sketch)
    
    # Ensure the output image is valid
    if output_image is None:
        print("Error: Output image is None!")
        return None

    # Return the processed sketch image
    return output_image

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

# Launch the Gradio app
interface.launch()