How does an pencil sketch application works ?

Shivamani A
4 min readJul 31, 2020

--

Learn the process of how an picture gets converted into a pencil sketched image.

Have you ever used any photo filter apps ??? Modern people are using various types of filter applications to edit raw photos into a good looking photos. Let us consider about converting a portrait picture (taken either in a digital single lens reflector camera or a phone camera) into a penciled sketched portrait picture. Generally if a portrait picture is need to be drawn, we need to stand still for a very long time till the artist finishes his portrait.

Portrait drawing

Today we don’t have that much time to sit around for several hours without doing nothing since we are all constantly trying to be productive. But we do also like to have sketched pictures of us such that the picture looks pretty. So how are we going to manage time and also get beautiful sketched pics? the answer is technology.

The developers have come to a solution of developing applications that converts a digital photo into a digital penciled sketch which helps the people to save time in just taking a snap and directly importing the picture into the application and converting it.

Example for a pencil sketch application
Example for pencil sketch application

But did anyone wondered how a software can do these drawings? How does it even understands it and get displayed digitally.

So lets go deep and see how it is done.

Basically, what we see in a screen is comprised full of pixels with which pictures and videos are viewed. you might wonder what a pixel is….?

A Pixel is a smallest form of information present in the screen which is used to display the information(details) of a picture or videos.

Now we might wonder how does a pixel does that???? well its not just a single pixel which does this it is possible when a certain number of pixels are combined together to represent the entire information on the screen. These pixels are present in your computer monitors, television(led, oled, lcd, mini led, etc), even in your smartphones.

Each pixel has mainly 3 colors namely red, green, and blue (RGB).The combination of these will make the pictures or videos to be presented accordingly on the screen. The pixels have a intensity range starting from 0 which means dark, till 255 which means bright.

Now that we are familiar with some basic concepts, Now lets see how the conversion works.

Let us create a program to convert an image to a pencil sketched image.

Here we are using python programming language to convert the image of a person into pencil sketched one as result.

we willbe using jupyter notebook as the programming platform.

python package needed:

  • cv2
  • matplotlib
  • sys(system)
Code for converting an image into a pencil sketched image.

We have have imported the image that is going to be converted into pencil sketched image. The imported image is stored in the variable img.

Now we are going to convert it into a grayscale image. Since the image is going to be a pencil sketched one so we don’t need the image to be in colored manner. Grayscale image is sort of like the black and white themed effect. We use cvtColor to convert the color image to convert the color image to gray image. Here in grayscale image the intensity which starts from Zero means black and 255 represents bright white.

we are reducing the image intensity to a uniform manner, so that the color correction can be easily managed.

The main trick is the Gaussian blur. Gaussin blur is also known as Gaussian smoothing is actually used to blur an image using the Gaussian function.the Gaussian blur is mainly used to reduce the noise from an image when processed.

Gaussian function applied at each dimension

In Gaussian blur, The degree of blurring is decided by the kernel size.

A Kernel size is like a matrix of pixels clustered together to blur the image in this case reduce the noise of the image.

Example of how the kernel size helps in bluring the image

The change in the kernel size increases or decreases the smoothing of the image, The lower the kernel size the image turns white due to less smoothing and higher value gives sharp image.

Having getting to know about how the code works, Its time to test the code.

let us give an image which needs to be converted into a penciled sketched image.

The image which is going to pencil sketched

the penciled sketched image is:

The pencil sketched image.

I Hope everyone have understood how the process of these image conversion is happening in every application we use to apply filter .

Thank you so much for reading this article and we shall meet in the next one.

Connect with me on linkedin:

www.linkedin.com/in/shivamani-a-4a2443199

--

--

No responses yet