Ad Code

Generate Pencil Sketch from Photo Using Python.


image - https://www.rawpixel.com/

The library we have to need in this project is OpenCV and we also use matplotlib in this project we will discuss matplotlib later.

If you have no library named OpenCV then you will be installing it on your python application or in your vs code terminal.

 pip3 install --upgrade opencv

Firstly we import the OpenCV library into your program

 import cv2
 import matplotlib.pyplot as plt

Now we read the image or put the location of the image(in the place of photo.jpg).

 img=cv2.imread("photo.jpg")
 Now showing the image file using CV2.
 cv2.imshow(‘original image’,img)
 cv2.waitKey(0)
 cv2.destroyAllWindows()

Now convert the image BGR to RGB:-

 RGB_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 plt.imshow(RGB_img)
 plt.axis(False)
 plt.show()

Converting Image to Pencil Sketch:-

 grey_img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 invert_img=cv2.bitwise_not(grey_img)
 #invert_img=255-grey_img 
 blur_img=cv2.GaussianBlur(invert_img, (111,111),0)
 invblur_img=cv2.bitwise_not(blur_img)
 #invblur_img=255-blur_im
 sketch_img=cv2.divide(grey_img,invblur_img, scale=256.0)
 cv2.imwrite(‘sketch.png’, sketch_img)
 cv2.imshow(‘sketch image’,sketch_img)
 cv2.waitKey(0)
 cv2.destroyAllWindows()


Orginal Image VS Pencil Sketch:-


 plt.fiure(figsize=(14,8))
 plt.subplot(1,2,1)
 plt.title('Original image', size=18)
 plt.imshow(RGB_img)
 plt.axis('off')
 plt.subplot(1,2,2)
 plt.title('Sketch', size=18)
 rgb_sketch=cv2.cvtColor(sketch_img, cv2.COLOR_BGR2RGB)
 plt.imshow(rgb_sketch)
 plt.axis('off')
 plt.show()


Thank You For Visit CodingFizz

Post a Comment

0 Comments

Ad Code