Pencil Sketch with Python

In this article, I’ll walk you through how to convert an image to a pencil sketch with Python in less than 20 lines of code. Python is a general-purpose programming language and with the growing popularity of Python, it can be used in any task today.

Image to Pencil Sketch with Python

Before we write any code, let’s go over some of the steps that will be used and try to understand them a bit. First, find an image that you want to convert to a pencil sketch with Python. I will be using the image of a puppy as you can see below.

Also, Read – Machine Learning Full Course for free.

dog
Source – https://www.wallpapertip.com/

Next, we need to read the image in RBG format and then convert it to a grayscale image. This will turn an image into a classic black and white photo.

Then the next thing to do is invert the grayscale image also called negative image, this will be our inverted grayscale image. Inversion can be used to enhance details.

Then we can finally create the pencil sketch by mixing the grayscale image with the inverted blurry image. This can be done by dividing the grayscale image by the inverted blurry image. Since images are just arrays, we can easily do this programmatically using the divide function from the cv2 library in Python.

Let’s Code

The only library we need for converting an image into a pencil sketch with Python is an OpenCV library in Python. It can be used by using the pip command; pip install opencv-python. But it is not imported by the same name. Let’s import it to get started with the task:

import cv2Code language: JavaScript (javascript)

I will not display the image at every step, if you want to display the image at every step to see the changes in the image then you need to use two commands; cv2.imshow(“Title You want to give”, Image) and then simply write cv2.waitKey(0). This will display the image.

Now the next thing to do is to read the image:

image = cv2.imread("dog.jpg")
cv2.imshow("Dog", image)
cv2.waitKey(0)Code language: JavaScript (javascript)

Now after reading the image, we will create a new image by converting the original image to greyscale:

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("New Dog", gray_image)
cv2.waitKey(0)Code language: JavaScript (javascript)

Now the next step is to invert the new grayscale image:

inverted_image = 255 - gray_image
cv2.imshow("Inverted", inverted_image)
cv2.waitKey()
Code language: JavaScript (javascript)

Now the next step in the process is to blur the image by using the Gaussian Function in OpenCV:

blurred = cv2.GaussianBlur(inverted_image, (21, 21), 0)Code language: Python (python)

Then the final step is to invert the blurred image, then we can easily convert the image into a pencil sketch:

inverted_blurred = 255 - blurred
pencil_sketch = cv2.divide(gray_image, inverted_blurred, scale=256.0)
cv2.imshow("Sketch", pencil_sketch)
cv2.waitKey(0)
Code language: JavaScript (javascript)
pencil sketch

And finally, if you want to have a look at both the original image and the pencil sketch then you can use the following commands:

cv2.imshow("original image", image)
cv2.imshow("pencil sketch", pencil_sketch)
cv2.waitKey(0)Code language: CSS (css)
output generated image

So this is how we can convert an image into a pencil sketch with Python. I hope you liked this article on how to convert an image into a pencil sketch with Python. Feel free to ask your valuable questions in the comments section below.

Also, Read – Google Search Algorithm with Python.

Follow Us:

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1498

2 Comments

Leave a Reply