sketch-image / app.py
Artmen's picture
Create app.py
090c9fa
import gradio as gr
import cv2
import os
title = "Convert image to sketch"
description = "This app uses OpenCV to convert an image to a pencil sketch. Simply upload an image, and the app will return a sketched version of the image."
ref = "Find the whole code [here](https://github.com/jazlopez/py-sketch-image/)."
def sketch(image, sigma_s, sigma_r, shade):
SIGMA_S= sigma_s
SIGMA_R= sigma_r
SHADE_FACTOR= shade
imageSizeX=image.shape[0]
imageSizeY=image.shape[1]
print('Processing image with size: x', imageSizeX, ', y', imageSizeY, '...')
imageToSketch = image
if imageSizeX > 3000 or imageSizeY > 3000:
print('Image too big, resizing to 1/6...')
imageToSketch = cv2.resize(imageToSketch, (int(imageSizeY/6), int(imageSizeX/6)), interpolation=cv2.INTER_AREA)
elif imageSizeX > 2000 or imageSizeY > 2000:
print('Image too big, resizing to 1/4...')
imageToSketch = cv2.resize(imageToSketch, (int(imageSizeY/4), int(imageSizeX/4)), interpolation=cv2.INTER_AREA)
elif imageSizeX > 1500 or imageSizeY > 1500:
print('Image too big, resizing to 1/3...')
imageToSketch = cv2.resize(imageToSketch, (int(imageSizeY/3), int(imageSizeX/3)), interpolation=cv2.INTER_AREA)
elif imageSizeX > 1000 or imageSizeY > 1000:
print('Image too big, resizing to 1/2...')
imageToSketch = cv2.resize(imageToSketch, (int(imageSizeY/2), int(imageSizeX/2)), interpolation=cv2.INTER_AREA)
try:
sk_gray, sk_color=cv2.pencilSketch(imageToSketch, sigma_s=SIGMA_S, sigma_r=SIGMA_R, shade_factor=SHADE_FACTOR)
print('Image processed successfully!')
return sk_gray
except:
print('Error processing image...')
return image
demo = gr.Interface(
fn=sketch,
inputs=[
gr.Image(),
gr.Slider(0, 100, step=1, value=50),
gr.Slider(0, 0.1, step=0.01, value=0.05),
gr.Slider(0, 0.1, step=0.01, value=0.05)
],
outputs=[gr.Image(height=500)],
title=title,
description=description,
article=ref,
examples=[
[os.path.join(os.path.dirname(__file__), "images/source.jpg")]
],
allow_flagging='never',
)
demo.queue().launch()