Coders Packet

CONVERT AN IMAGE TO A SKETCH USING PYTHON'S OPENCV LIBRARY

By RAMYA RAJESWARI VISSAPRAGADA

This is a very efficient program where we use opencv of python to convert any image to a gray scale sketch

CONVERTING ANY IMAGE TO A SKETCH OF GRAYSCALE USING OPENCV LIBRARY OF PYTHON:

 

OPENCV LIBRARY: 

It is a huge open-source library that is used to perform complex technological stuff like:

Computer Vision

Machine Learning and

Image Processing etc.

So using this library we can easily analyze and perform various operations to our images and its world revolves mainly around the concept of IMAGES.

It can be installed using the command: pip install OpenCV

 

CONVERTING IMAGE TO SKETCH PROCEDURE USING PYTHON PROGRAM:

Firstly, we have to install OpenCV library into our IDE ( Integrated Development Environment ) and then we have to import it into our python program.

After that, we have to read our image file.

NOTE: We have to download the image file we want to convert into the location of our Python Directory on our systems.

After reading, we perform few conversions like:

Converting the Image from RGB format (RED_GREEB_BLUE FORMAT) to Gray_Scale format using the cv2.COLOR_BGR2GRAY parameter in its Convert color function.

Then we convert our image into inverted format.

Next, we add some blurred effects to our inverted image using the GaussianBlurr function

Then we combine these versions of inverted and grayscale images which will ultimately generate our sketch of the desired image.

This program is very simple with limited code, with efficient results.

SOURCE CODE:

import cv2
img = cv2.imread("minion.jpg")

grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
invert_img = cv2.bitwise_not(grey_img)

blurr = cv2.GaussianBlur(invert_img, (21, 21), 0)
inverted_blur = cv2.bitwise_not(blurr)

sketch = cv2.divide(grey_img, inverted_blur, scale = 256.0)

cv2.imwrite("sketch.png", sketch)

 

OUTPUT:

NOTE: We can name our image anything with its given extension, I have named mine as minion.jpg and also we can designate our output sketch name of our choice. In my case, I have names mine as sketch.png

 

 

Download project

Reviews Report

Submitted by RAMYA RAJESWARI VISSAPRAGADA (ramyavissapragada)

Download packets of source code on Coders Packet