Pencil Sketch with Python

Parveen Kumar
4 min readJun 14, 2021

--

Are you curious to know about innovative projects using Python? Then you are in the right place to boost up your knowledge. In this article I’m going to present the precise details of how to convert any image to a beautiful pencil sketch using Python OpenCV. From this project you will get the concepts to edit an image as per your choice just by coding. So now to build up this project and understand the code first you have to know about computer vision and the OpenCV module of Python.

What is Computer Vision?

Computer Vision is a subset of Artificial Intelligence that trains computers to interpret and understand the visual world. Using digital images from cameras and videos and deep learning models, computers can accurately identify and classify object, derive meaningful information and then they take actions or make recommendations based on that information. Nowadays it’s being used in lots of many complex fields like –

· Defect detection

· Metrology

· Assembly verification

· Screen reader

· Automated media coverage

· Cancer detection

· Automatic harvesting etc.

What is OpenCV?

OpenCV is basically a library of used for real-time Computer Vision. It supports a wide range of programming languages like Python, C, C++, Java etc and also supports different platforms including Windows, Linux, MacOS. By using it, one can process images and videos to identify objects, faces or even handwriting of a human. When it integrated with various libraries, such as Numpy, Python is capable of processing the OpenCV array structure for analysis. To identify image pattern and its various features we use vector space and perform mathematical operations on these features.

Summary of the project:

Before start coding, let’s take a look into the steps to convert an image to a pencil sketch.

I. First, select an image as per your choice which you would like to convert to pencil sketch.

II. Then read the image in RGB format.

III. Now, convert it to a grayscale image to make the image black and white.

IV. Invert the grayscale image and make it negative.

V. Blur the inverted image using GaussianBlur function.

VI. Again invert the blurred image.

VII. Now, to finally create the pencil sketch, mix up the grayscale image and inverted blurry image by dividing the grayscale image by the inverted blurry image.

How to code:

In this project, we will use the OpenCV library of Python. It can be used by using the pip command; pip install opencv-python.

· To get started first we have to import the OpenCV library named as cv2.

#This project converts any image into a pencil sketch.
#import the library
import cv2

· Now we will read the image in RGB format by using imread function for further modification.

#read the image to further edit it
img = cv2.imread("spiderman.jpg")

· After reading, convert the image into grayscale by the cvtColor function and we have to pass 2 parameters here- the variable where the image is stored after reading(i.e. img) and cv2.COLOR_BGR2GRAY.

#convert the image to grayscale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

· Now invert the grayscale image.

#invert the grayscale image
inverted_img = 255-gray_img

· Blur the inverted image by GaussianBlur function of OpenCV.

#blur the inverted image
blurred_img = cv2.GaussianBlur(inverted_img, (21, 21), 0)

· Again invert the blurred image for good detailing.

#invert the blurred image
inverted_blurred_img = 255-blurred_img

· Now, to make the pencil sketch we have to divide the grayscale image by inverted blurry image by using the divide function where we will pass 3 parameters- variable where grayscale image is stored, variable where inverted blurry image is stored and set the scale to 256.0.

#Create the pencil sketch
pencil_sketch = cv2.divide(gray_img, inverted_blurred_img, scale=256.0)

· Now to show the original image and pencil sketch simultaneously we will use imshow function and will also give an infinite delay by using the waitKey(0) function.

#show the original image and the pencil sketch
cv2.imshow("Original image", img)
cv2.imshow("Pencil sketch", pencil_sketch)
cv2.waitKey(0)

PENCIL SKETCH WITH PYTHON CODE

#This project converts any image into a pencil sketch.
#import the library
import cv2
#read the image to further edit it
img = cv2.imread(“spiderman.jpg”)

#convert the image to grayscale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


#invert the grayscale image
inverted_img = 255-gray_img

#blur the inverted image
blurred_img = cv2.GaussianBlur(inverted_img, (21, 21), 0)

#invert the blurred image
inverted_blurred_img = 255-blurred_img

#Create the pencil sketch
pencil_sketch = cv2.divide(gray_img, inverted_blurred_img, scale=256.0)

#show the original image and the pencil sketch
cv2.imshow(“Original image”, img)
cv2.imshow(“Pencil sketch”, pencil_sketch)
cv2.waitKey(0)

So, the output will be like-

So we can make our first innovative project in Python by following the above steps. I hope you like it. Stay tuned with us to get more updates about new projects of Python.

--

--

No responses yet