How to convert an image to a sketch image on Python

How to convert an image to a sketch image on Python

Hi everyone,

Welcome back to my blog. In this post, I will show you how to convert an image to a sketch image in a few lines of code on python.

This is another simple project on python, that could help improve your python programming skills.

Lets get started:

Line 1:

pip install opencv-python

Firstly, we need to make sure the opencv library is installed in the machine, if not installed we can install it using pip.

Line 2:

import cv2

Then we need to import the opencv library by using the above line.

Line 3 & 4:

image1 = cv2.imread('mypic.jpeg')
window_name='Actual image'

Line 3, we are specifiying the path to the image and line 4 is to give a name to the window when printing the images.

Line 5:

cv2.imshow(window_name, image1)

This wil display the actual image.

Line 6 & 7:

grey_img = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
invert = cv2.bitwise_not(grey_img)

Line 6 & 7, we are using the cvtColor function to convert the image from RGB to Gray. Then we are inverting the image color.

Line 8, 9 & 10:

blur = cv2.GaussianBlur(invert, (21, 21), 0)
invertedblur = cv2.bitwise_not(blur)
sketch = cv2.divide(grey_img, invertedblur, scale=256.0)
  • Line 8, we are using the Gaussian filter to smoothen the image. This process helps us to perform more complex operations as the GaussianBlur reduces noise or unwanted variances of the image.
  • Line 9, we can use the bitwise_not method to which perform a bit-wise NOT operation. This operation is used for image manipulation by extracting essential parts in the image.
  • Line 10, we use the divide function with the scale parameter of 256.0 to sketch the image.

Line 11:

cv2.imwrite("mypicsketch.jpeg", sketch)

Here, we are saving the new sketch image in the same directory.

Line 12:

image = cv2.imread("mypicsketch.jpeg")

Here we are specifying the path of the new sketch image that has been generated from the previous lines of code.

Line 13:

window_name ='Sketch image'

In this line we are given a name to the window when printing the images.

Line 14:

cv2.imshow(window_name, image)

This will display the new image.

All the code combined:

pip install opencv-python
import cv2

image1 = cv2.imread('mypic.jpeg')
window_name='Actual image'

cv2.imshow(window_name, image1)

grey_img = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
invert = cv2.bitwise_not(grey_img)

blur = cv2.GaussianBlur(invert, (21, 21), 0)
invertedblur = cv2.bitwise_not(blur)
sketch = cv2.divide(grey_img, invertedblur, scale=256.0)

cv2.imwrite("mypicsketch.jpeg", sketch)

image = cv2.imread("mypicsketch.jpeg")

window_name ='Sketch image'

cv2.imshow(window_name, image)

cv2.waitKey(0)

Lets see the output now

Screenshot 2022-07-02 at 16.29.49.png

Congratulations

In this tutorial, you learned how to convert an image to a sketch image on Python.

I hope you enjoyed this tutorial. Please make sure to see my other contents :).

Did you find this article valuable?

Support Hussain Hussaini by becoming a sponsor. Any amount is appreciated!