Convert a photo to different types of sketches using python

ACES PVGCOET
1 Hour Blog Series
Published in
5 min readMar 29, 2021

Introduction:

In this blog, I will be showing how to change an image in jpg, jpeg, etc. format to different types of sketches such as watercolor sketch, pencil sketch using the gaussian formula.

Python is simple with an easily readable syntax and that makes it well-loved by both seasoned developers and experimental students. Python is also supremely efficient. It has hundreds of different libraries and frameworks that can be used by developers. These libraries and frameworks are really useful in saving time which in turn makes Python even more popular.

Components:

This blog mainly contains three parts A1, A2 and A3.

PART A1: Converting an image into a pencil sketch using OpenCV.

PART A2: Converting an image into a watercolor sketch using OpenCV.

PART A3: Converting an image into a textured sketch.

Pre-requisites:

  1. Python3 for the concerned operating system

https://www.python.org/downloads/

2. OpenCV module

https://www.javatpoint.com/how-to-install-opencv-in-python

What is openCV?

-OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library.

The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision and machine learning algorithms. These algorithms can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, track moving objects, extract 3D models of objects, produce 3D point clouds from stereo cameras, stitch images together to produce a high resolution image of an entire scene, find similar images from an image database, remove red eyes from images taken using flash, follow eye movements, recognize scenery and establish markers to overlay it with augmented reality, etc.

PART A1 — Converting an image into a pencil sketch using OpenCV.

Expected Results:

Steps to follow:

1. Convert the RGB colour image to grayscale.

2. Invert the grayscale image to get a negative.

3. Apply a Gaussian blur to the negative from step 2.

4. Blend the grayscale image from step 1 with the blurred negative from step

Working:

Step 1:

Import the required libraries OpenCV, matplotlib.

import cv2import matplotlib.pyplot as plt

Step 2:

Now, read the image using imread function. The image filename that I have used is travel.jpg

img = cv2.imread('travel.jpg')

Step 3:

We will now create a new image from this img by converting the original image to RGB scale using cv2.cvtColor function and then show the image.

Explaination : There is a difference in pixel ordering in OpenCV and Matplotlib. OpenCV follows BGR order, while matplotlib likely follows RGB order. Therefore, when we display an image loaded in OpenCV using matplotlib functions, we may want to convert it into RGB mode.

original_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)plt.axis("off")plt.imshow(original_img)<matplotlib.image.AxesImage at 0x1e1149059d0>

Step 5:

Now, we have to invert the grayscale image using the bitwise_not function.

plt.axis(“off”)img_invert = cv2.bitwise_not(gray_img)plt.imshow(img_invert, cmap=”gray”, vmin=0, vmax=255)<matplotlib.image.AxesImage at 0x1e1147c4a00>

Step 6:

The next step is to use the Gaussian function to blur out the image. It is a widely used effect in graphics software, typically to reduce image noise and reduce detail.

plt.axis("off")img_smoothing = cv2.GaussianBlur(img_invert, (21, 21),sigmaX=0, sigmaY=0)plt.imshow(img_smoothing, cmap="gray", vmin=0, vmax=255)<matplotlib.image.AxesImage at 0x2d73fde0e50>

Step 7:

The Final step is to create a pencil sketch. To do so, we will write a function named ‘dodge’ to divide the inverted and gaussian blurred images and saving is as our final image.

We have to further use the cv2.imwrite function to store our final image with a proper name (here, sketch.png).

def dodge(x, y):return cv2.divide(x, 255 - y, scale=250)plt.axis("off")final_img = dodge(gray_img, img_smoothing)plt.imshow(final_img, cmap="gray", vmin=200, vmax=255)cv2.imwrite('sketch.png',final_img)True

PART A2 — Convert image into a watercolor sketch.

Expected Results

Steps to follow:

1. Convert the image from BGR to RGB scale.

2. Use the stylization function of OpenCV to convert an image into its equivalent watercolour sketch.

Working:

Follow PART A1 for STEPS 1,2 & 3.

Step 4:

We will use the stylization function (cv2.stylization) from OpenCV to convert our image into a watercolour sketch.

Finally, write the image with the specific file name and store it using cv2.imwrite function.

plt.axis("off")watercolour_image = cv2.stylization(original_img, sigma_s=100, sigma_r=0.45)plt.imshow(watercolour_image)cv2.imwrite('sketch1.png',watercolour_image)True

PART A3 — Convert image into a textured sketch.

Expected Results:

Steps to follow:

1. Convert the image from BGR to RGB scale.

2. Sharpen the image to get a detailed sketch.

3. Convert the image into a textured sketch and save.

Working:

Follow PART A1 for STEPS 1,2 & 3.

Step 4:

We will use the detailEnhance function (cv2.detailEnhance) from OpenCV to sharpen the details of our image.

plt.axis("off")sharp_img = cv2.detailEnhance(original_img,sigma_s=40,sigma_r=0.8)plt.imshow(sharp_img)<matplotlib.image.AxesImage at 0x2d745d105b0>

Step 5:

Next, we will use the pencilSketch function from OpenCV to get a textured image.

Finally, write the image with the specific file name and store it using cv2.imwrite function.

plt.axis("off")tex_gray,tex_color = cv2.pencilSketch(sharp_img, sigma_s=10, sigma_r=0.40, shade_factor=0.02)plt.imshow(tex_color)True

Applications:

The sketching functions of python can be found to be used in photo filter apps such as prisma, picsart, etc. to add special effects to photographs.

Conclusion:

So, we saw that making art with OpenCV is a piece of cake especially using the inbuilt functions. We are now able to convert any image into a cool sketch or water color art or even a textured sketch.

I hope you enjoyed the article.

This article was contributed by Shreya Gore
LinkedIn profile — https://www.linkedin.com/in/shreya-gore-3042911aa/

--

--