Make beautiful pencil sketches using OpenCV in 9 lines of code

Make beautiful pencil sketches using OpenCV in 9 lines of code

Subscribe to my newsletter and never miss my upcoming articles

For this simple project we will use OpenCV in Python.

It is a library using which we can develop real-time computer vision applications. It mainly focuses on image processing, video capture and analysis including features like face detection and object detection.

But before that....

A bit of backstory

When I was in 7th grade (3 years ago), I used to play with this photo editing software called " GIMP ". (short for GNU Image Manipulation Software)

Using a couple of clever techniques, I would make sketches out of images like what you saw in the thumbnail.

Let's see how this process works.

  • You take an image


  • Convert it to grayscale


  • Invert the grayscaled image


  • Blur the inverted image


  • Subtract the grayscaled image from the blurred inverted image

et voilà !


I figured that I could automate this process using OpenCV, let see the code and how it works.

import cv2
# Importing the OpenCV libarary

img = cv2.imread('image.jpg')
#Reading the image

img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Converting the image to grayscale

img_invert = cv2.bitwise_not(img_gray)
#Inverting the grayscaled image

img_smoothing = cv2.GaussianBlur(img_invert, (21, 21),sigmaX=0, sigmaY=0)
#Blurring the inverted image

def dodge(x, y):
    return cv2.divide(x, 255 - y, scale=256)
final_img = dodge(img_gray, img_smoothing)
#Subtracting the blurred iamge from the orignal image

cv2.imwrite('img.jpg', final_img)
#Writing the final output to a a file

Just like that in 9 lines of code, you can easily create skteches using OpenCV and Python!

Here's the Github repository with this code .

Interested in reading more such articles from Pratham Prasoon?

Support the author by donating an amount of your choice.

Recent sponsors
Share this