Pencil Sketch Effect
Everyone loves creating a sketch or potraits of their pictures with great details.
Here we are with another visual effect The Pencil Sketch effect. This project demonstrates how to
apply such kind of effect in an image.
This is a good approache towards making some good pencil sketches.
This projects involves various methods like image sharpening, applying grayscale, gaussian blurs and
invertion of an image.This project is a combination of our previous basic projects.
Tools and Languages:
Installation
Use the package manager pip to install cv2 and numpy.
pip install cv2
pip install numpy
Import
Use import keyword to import modules.
import cv2
import numpy as np
Reading image from file
img = cv2.imread("cat.png")
Concepts and steps :
1.Sharpen the original image.
Sharpening:
kernel_sharpening = np.array([[-1,-1,-1],
[-1, 9,-1],
[-1,-1,-1]])
sharpened = cv2.filter2D(img,-1,kernel_sharpening)
2.Convert sharpened image into Gray scale.
Grayscale:
gray = cv2.cvtColor(sharpened , cv2.COLOR_BGR2GRAY)
3.Generate the negative or inverted image of grayscaled & store it.
Invertion:
inv = 255-gray
4.Apply Gaussian blur to the inverted image.
Gaussian blur:
gaussgray = cv2.GaussianBlur(inv,ksize=(15,15),sigmaX=0,sigmaY=0)
Now we have gaussian blured image of inverted image and also gray image as well.In-order to generate pencil-sketh
we need to merge both inverted image and gray image.In-order to do so we will b using improved dodging method
defined below.
def dodgeV2(image,mask):
return cv2.divide(image,255-mask,scale=256)
Now we need to apply this to our image
pencil_img = dodgeV2(gray,gaussgray)
Completion message
print('Pencil Sketch effect Applied.')
Comparing original vs sketch
cv2.imshow('ORIGINAL',img)
cv2.imshow('PENCIL-SKETCH',pencil_img)
cv2.waitKey(0)
cv2.destroyAllWindows()