File size: 4,480 Bytes
f45aefd
50e7fdf
 
 
 
 
 
 
f45aefd
d835849
 
 
f45aefd
1e24872
a1c4898
4ad0dc0
 
 
a1c4898
 
 
4ad0dc0
 
 
 
 
a1c4898
 
 
 
 
 
 
4ad0dc0
a1c4898
 
 
 
 
 
 
 
 
 
 
4ad0dc0
a1c4898
 
 
4ad0dc0
a1c4898
 
 
4ad0dc0
a1c4898
 
 
 
 
4ad0dc0
 
a1c4898
d835849
1e24872
 
a1c4898
 
 
 
 
4ad0dc0
 
 
 
 
a1c4898
 
4ad0dc0
a1c4898
 
 
 
 
 
 
4ad0dc0
 
a1c4898
1e24872
d835849
1e24872
a1c4898
 
d707b83
a1c4898
 
4ad0dc0
 
 
a1c4898
 
 
 
 
 
 
 
 
4ad0dc0
a1c4898
 
 
 
 
4ad0dc0
 
d707b83
f45aefd
d835849
 
 
6c92a72
5312728
a1c4898
 
 
6c92a72
a1c4898
e7ca56f
5312728
d835849
 
 
a1c4898
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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):
    try:
        # Add debug print
        print(f"Input image shape: {np.array(img).shape}")
        
        # Convert PIL Image to numpy array (BGR format for OpenCV)
        img = np.array(img)
        
        # Check if image is empty
        if img is None or img.size == 0:
            print("Error: Empty input image")
            return None
            
        # Ensure blur_strength is odd
        blur_strength = max(1, int(blur_strength))
        if blur_strength % 2 == 0:
            blur_strength += 1
            
        # Convert the image to grayscale
        img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        print(f"Grayscale shape: {img_gray.shape}")
        
        # Invert the grayscale image
        img_inverted = 255 - img_gray
        
        # Apply Gaussian blur to the inverted image
        img_blur = cv2.GaussianBlur(img_inverted, (blur_strength, blur_strength), 0)
        
        # Blend the grayscale and blurred inverted images
        denominator = 255 - img_blur
        denominator[denominator == 0] = 1  # Prevent division by zero
        
        img_blend = (img_gray * 256.0) / denominator
        
        # Clip values to valid range
        img_blend = np.clip(img_blend, 0, 255).astype(np.uint8)
        print(f"Blend shape: {img_blend.shape}")
        
        # Adjust brightness and contrast
        sketch_with_bg = adjust_brightness_contrast(img_blend, brightness, contrast)
        print(f"Final output shape: {sketch_with_bg.shape}")
        
        return sketch_with_bg
        
    except Exception as e:
        print(f"Error in convert_to_sketch: {str(e)}")
        import traceback
        traceback.print_exc()
        return None

# Function to adjust brightness and contrast
def adjust_brightness_contrast(img, brightness, contrast):
    try:
        # Ensure brightness is within valid range
        brightness = float(brightness)
        contrast = float(contrast)
        
        # Convert to float32 for calculations
        img = img.astype(float)
        
        # Apply contrast
        img = img * contrast
        
        # Apply brightness
        img = img + brightness
            
        # Ensure output is in valid range
        img = np.clip(img, 0, 255).astype(np.uint8)
        return img
        
    except Exception as e:
        print(f"Error in adjust_brightness_contrast: {str(e)}")
        import traceback
        traceback.print_exc()
        return None

# Gradio interface function
def sketch_interface(image, blur_strength, brightness, contrast):
    if image is None:
        print("Error: No input image provided!")
        return None
        
    try:
        print(f"Input image type: {type(image)}")
        print(f"Input image size: {image.size}")
        
        # Convert the input image to a sketch with adjustments
        sketch = convert_to_sketch(image, blur_strength, brightness, contrast)
        
        if sketch is None:
            print("Error: Sketch conversion failed!")
            return None
            
        # Convert the processed numpy array back to a PIL Image
        output_image = Image.fromarray(sketch)
        print(f"Output image size: {output_image.size}")
        
        return output_image
        
    except Exception as e:
        print(f"Error in sketch_interface: {str(e)}")
        import traceback
        traceback.print_exc()
        return None

# Create Gradio interface
interface = gr.Interface(
    fn=sketch_interface,
    inputs=[
        gr.Image(type="pil", label="Upload Image"),
        gr.Slider(minimum=1, maximum=51, step=2, value=21, label="Blur Strength"),
        gr.Slider(minimum=-100, maximum=100, value=0, label="Brightness"),
        gr.Slider(minimum=0.1, maximum=3, step=0.1, value=1, label="Contrast")
    ],
    outputs=gr.Image(type="pil", label="Sketch Output"),
    title="Image to coloring page 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()