Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
def image_to_sketch(image):
|
| 7 |
+
img = np.array(image.convert("RGB"))
|
| 8 |
+
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 9 |
+
inverted = 255 - gray
|
| 10 |
+
blurred = cv2.GaussianBlur(inverted, (21, 21), 0)
|
| 11 |
+
|
| 12 |
+
def dodge(front, back):
|
| 13 |
+
return cv2.divide(front, 255 - back, scale=255)
|
| 14 |
+
|
| 15 |
+
sketch = dodge(gray, blurred)
|
| 16 |
+
return [image, Image.fromarray(sketch)]
|
| 17 |
+
|
| 18 |
+
# Create Gradio interface
|
| 19 |
+
demo = gr.Interface(
|
| 20 |
+
fn=image_to_sketch,
|
| 21 |
+
inputs=gr.Image(type="pil", label="Upload a portrait image"),
|
| 22 |
+
outputs=[
|
| 23 |
+
gr.Image(label="Original"),
|
| 24 |
+
gr.Image(label="Sketch"),
|
| 25 |
+
],
|
| 26 |
+
title="🖌️ Image to Sketch Converter",
|
| 27 |
+
description="Upload a portrait image and watch it turn into a pencil sketch!",
|
| 28 |
+
examples=None
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
demo.launch()
|