DEV Community

Cover image for Convert Images Into Pencil Sketch Using Python.
Ebikara Spiff
Ebikara Spiff

Posted on • Edited on

Convert Images Into Pencil Sketch Using Python.

Introduction

Have you ever felt like your photos needed a little extra touch to stand out? Well, get ready because we're about to learn a cool Python trick! We're going to take ordinary photos and turn them into awesome pencil sketches using Python and OpenCV. This will make your pictures look like they were drawn by hand!

Imagine being able to give your photos a unique artistic touch that makes them pop. Whether you're a tech guru or just someone who loves playing around with pictures, this tutorial is perfect for you. We'll guide you through each step, from setting up your computer to understanding how to use OpenCV.

Get ready to see your photos transform as we combine technology and creativity.

Setting Up The Project Environment

When working in Visual Studio Code (VS Code), create a new Python file for our Image to Pencil Sketch Conversion project.
It's helpful to have separate files for different parts of your project.

Create a new Python application:

To do this, you can start by opening your VS Code and creating a new folder:

Step 1

Open VS Code

Opening VS Code

Step 2

Create new folder

creating new folder in VS code

Step 3

Head over to the newly created folder and create a new file called app.py.

image of a python file in VS Code

Building the Image to Pencil Sketch Converter

We will convert the image below into a cool pencil sketch using OpenCV.

Original image

Step 1

We begin by importing the necessary library - OpenCV in Python as cv2 which provides us a wide range of functions for image processing tasks:



Import cv2


Enter fullscreen mode Exit fullscreen mode

Step 2

Here we will initialize a variable image and read the image file which we want to convert to a pencil sketch using the cv2.imread function:



Image = cv2.imread


Enter fullscreen mode Exit fullscreen mode

Step 3

Next, we will specify the file name of the image to be processed, to ensure that the correct image is loaded for further manipulation:



Image = cv2.imread(captain.jpg)


Enter fullscreen mode Exit fullscreen mode

Step 4

In this step, we want to convert the image to grayscale, this simplifies the image processing task and prepares the image for the pencil sketch effect.
To do this we will create a variable ‘grey_image’ to add a grey filter to our image using cv2.cvtColor function and specifying the conversion code cv2.COLOR_BGR2GRAY:



grey_image = cv2.cvtColor(image, cv2.COLOR_BGR2GAY)


Enter fullscreen mode Exit fullscreen mode

Step 5

Next, we have to invert our image using cv2.bitwise_not function to prepare it for the pencil sketch effect.
To do this we will create another variable ‘invert’:



Invert = cv2.bitwise_not(grey_img)


Enter fullscreen mode Exit fullscreen mode

Step 6

In this step, we will add a Gaussian blur to the inverted image using cv2.GaussianBlur to smoothen the image and remove noise:



Blur= cv2.GaussianBlur(invert, (21,21),0)


Enter fullscreen mode Exit fullscreen mode

Step 7

Next, we will have to Invert our blurred image to get a clearer outline of objects in the image:



Invertedblur = cv2.bitwise_not(blur)


Enter fullscreen mode Exit fullscreen mode

Step 8

Lastly, we create our final pencil sketch effect by dividing the grayscale image by the inverted blurred image using cv2.divide to give the image a hand-drawn appearance:



Sketch = cv2.divide(grey_img, invertedblur, scale=256.0)


Enter fullscreen mode Exit fullscreen mode

Step 9

Finally, we will save our sketch image file as a PNG file sketch.png to ensure that the processed image can be accessed and viewed separately from the original:



Cv2.imwrite(sketch.png , sketch)


Enter fullscreen mode Exit fullscreen mode

Step 10

Save and run to execute the code to generate the pencil sketch image.

Image of the pencil sketch

Conclusion

The process of creating a pencil sketch effect using OpenCV involves several key steps. Initially, we convert the original image to a grayscale format and then proceed to create a blurred version of this grayscale image using a specified kernel size.

Subsequently, we invert the blurred image to prepare for the final step, where we generate the pencil sketch effect. This effect is achieved by dividing the grayscale image by the inverted blurred image using the cv2.divide function. The resulting image will exhibit a hand-drawn appearance, reminiscent of a traditional pencil sketch. To ensure that the processed sketch image can be accessed and viewed separately from the original, we save it as a PNG file named sketch.png using the cv2.imwrite method.

By following these steps and executing the code, we can successfully generate a visually appealing pencil sketch image that can be used for various creative purposes.

Top comments (0)