Convert Images Into Pencil Sketch Using Python.
Tags: webdev, beginners, programming, python
Published: 2024-04-11 21:42:20

## 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](https://www.python.org/) and [OpenCV](https://pypi.org/project/opencv-python/). 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](https://pypi.org/project/opencv-python/). 

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

## Setting Up The Project Environment

When working in [Visual Studio Code](https://code.visualstudio.com/) (VS Code), create a new [Python](https://www.python.org/) 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](https://code.visualstudio.com/) and creating a new folder:


####Step 1

Open VS Code 



![Opening VS Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhw80u0gmkwrmzj3410n.png)




####Step 2

Create new folder



![creating new folder in VS code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wl435axq68fi7iytzrwh.png)



####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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h6b563ybvkotsxqtwrqx.png)




##Building the Image to Pencil Sketch Converter

We will convert the image below into a cool pencil sketch using [OpenCV](https://pypi.org/project/opencv-python/). 


![Original image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6wwx1mmbjpgum3aitj0s.png)



###Step 1

We begin by importing the necessary library - [OpenCV](https://pypi.org/project/opencv-python/) in Python as [cv2](https://pypi.org/project/opencv-python/) which provides us a wide range of functions for image processing tasks:

<prev>
```python
Import cv2
```
</prev>

###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:

<prev>
```python
Image = cv2.imread
```
</prev>

###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:

<prev>
```python
Image = cv2.imread(“captain.jpg”)
```
</prev>

###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`:

<prev>
```python
grey_image = cv2.cvtColor(image, cv2.COLOR_BGR2GAY)
```
</prev>

###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’:

<prev>
```python
Invert = cv2.bitwise_not(grey_img)
```
</prev>

###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:

<prev>
```python
Blur= cv2.GaussianBlur(invert, (21,21),0)
```
</prev>

###Step 7

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

<prev>
```python
Invertedblur = cv2.bitwise_not(blur)
```
</prev>

###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:

<prev>
```python
Sketch = cv2.divide(grey_img, invertedblur, scale=256.0)
```
</prev>

###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:

<prev>
```python
Cv2.imwrite(“sketch.png” , sketch)
```
</prev>

###Step 10

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


![Image of the pencil sketch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3ujcohsmou0boa7aghp.png)



##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.