Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# Attempt to import
|
2 |
try:
|
3 |
import cv2
|
4 |
except ImportError:
|
@@ -6,18 +6,18 @@ except ImportError:
|
|
6 |
subprocess.check_call(["pip", "install", "opencv-python-headless"])
|
7 |
import cv2
|
8 |
|
9 |
-
from PIL import Image
|
10 |
import numpy as np
|
11 |
import gradio as gr
|
12 |
import os
|
13 |
|
14 |
-
# Function to convert image to sketch
|
15 |
def convert_to_sketch(img, blur_strength, brightness, contrast):
|
16 |
# Convert PIL Image to numpy array
|
17 |
img = np.array(img)
|
18 |
|
19 |
# Convert the image to grayscale
|
20 |
-
img_gray = cv2.cvtColor(img, cv2.
|
21 |
|
22 |
# Invert the grayscale image
|
23 |
img_inverted = cv2.bitwise_not(img_gray)
|
@@ -37,7 +37,6 @@ def convert_to_sketch(img, blur_strength, brightness, contrast):
|
|
37 |
# Adjust brightness and contrast
|
38 |
sketch_with_bg = adjust_brightness_contrast(sketch_with_bg, brightness, contrast)
|
39 |
|
40 |
-
# Return the adjusted sketch image
|
41 |
return sketch_with_bg
|
42 |
|
43 |
# Function to adjust brightness and contrast
|
@@ -51,24 +50,24 @@ def sketch_interface(image, blur_strength, brightness, contrast):
|
|
51 |
# Convert the input image to a sketch with adjustments
|
52 |
sketch = convert_to_sketch(image, blur_strength, brightness, contrast)
|
53 |
|
54 |
-
#
|
55 |
output_image = Image.fromarray(sketch)
|
56 |
|
57 |
-
# Return the processed sketch image
|
58 |
return output_image
|
59 |
|
60 |
# Create Gradio interface
|
61 |
interface = gr.Interface(
|
62 |
fn=sketch_interface,
|
63 |
inputs=[
|
64 |
-
gr.Image(type="pil", label="Upload Image"
|
65 |
gr.Slider(1, 51, step=2, label="Blur Strength", value=21),
|
66 |
gr.Slider(-100, 100, label="Brightness", value=0),
|
67 |
gr.Slider(1, 3, step=0.1, label="Contrast", value=1)
|
68 |
],
|
69 |
-
outputs=
|
70 |
title="Cartoon to Sketch Converter",
|
71 |
-
description="Upload an image
|
72 |
)
|
73 |
|
74 |
# Launch the Gradio app
|
|
|
1 |
+
# Attempt to import opencv-python-headless and install it if not available
|
2 |
try:
|
3 |
import cv2
|
4 |
except ImportError:
|
|
|
6 |
subprocess.check_call(["pip", "install", "opencv-python-headless"])
|
7 |
import cv2
|
8 |
|
9 |
+
from PIL import Image, ImageTk
|
10 |
import numpy as np
|
11 |
import gradio as gr
|
12 |
import os
|
13 |
|
14 |
+
# Function to convert the image to a sketch
|
15 |
def convert_to_sketch(img, blur_strength, brightness, contrast):
|
16 |
# Convert PIL Image to numpy array
|
17 |
img = np.array(img)
|
18 |
|
19 |
# Convert the image to grayscale
|
20 |
+
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
21 |
|
22 |
# Invert the grayscale image
|
23 |
img_inverted = cv2.bitwise_not(img_gray)
|
|
|
37 |
# Adjust brightness and contrast
|
38 |
sketch_with_bg = adjust_brightness_contrast(sketch_with_bg, brightness, contrast)
|
39 |
|
|
|
40 |
return sketch_with_bg
|
41 |
|
42 |
# Function to adjust brightness and contrast
|
|
|
50 |
# Convert the input image to a sketch with adjustments
|
51 |
sketch = convert_to_sketch(image, blur_strength, brightness, contrast)
|
52 |
|
53 |
+
# Ensure the output is a valid image (PIL Image)
|
54 |
output_image = Image.fromarray(sketch)
|
55 |
|
56 |
+
# Return the processed sketch image
|
57 |
return output_image
|
58 |
|
59 |
# Create Gradio interface
|
60 |
interface = gr.Interface(
|
61 |
fn=sketch_interface,
|
62 |
inputs=[
|
63 |
+
gr.Image(type="pil", label="Upload Image"),
|
64 |
gr.Slider(1, 51, step=2, label="Blur Strength", value=21),
|
65 |
gr.Slider(-100, 100, label="Brightness", value=0),
|
66 |
gr.Slider(1, 3, step=0.1, label="Contrast", value=1)
|
67 |
],
|
68 |
+
outputs=gr.Image(type="pil", label="Sketch Output"),
|
69 |
title="Cartoon to Sketch Converter",
|
70 |
+
description="Upload an image to convert it into a sketch, adjust the blur strength, brightness, and contrast for different effects."
|
71 |
)
|
72 |
|
73 |
# Launch the Gradio app
|