27.4 C
Gujarat
HomepythonConvert Images Into Pencil Sketch Using Python

Convert Images Into Pencil Sketch Using Python

In this python project, we will convert an image into a sketch file or add a sketch filter using python OpenCV.  This project will be fun, why? Because we will create this python program in just 5 lines.

Convert Images Into Pencil Sketch Using Python
So we will use a filter in python in just 4 simple steps.First, we have to add a grey filter to our image. Then we have to invert the filter to the grey image we have created. and then we have to add a blur effect to our inverted image. And after we create a sketch image.

so let’s get started now open your favorite code editor. And create a python file. After first import the OpenCV library like import cv2. And create an Image variable to store our image. After creating a grey_img variable for the add grey filter to our image. After adding a grey filter we have to invert our image. So create another variable invert. Then invert the image After adding a blur filter. So we create another variable blur. After adding a blur effect. Invert this blur effect.
so again create a variable inverted_blur. And last we have to add a filter which is a sketch. so create a sketch variable and call cv2.divide(grey_img,inverted_blur,scale=256.0)
Now we have to save our sketch image 
cv2.imwrite(“sketch.png”,sketch)

Note: make sure your file extension name is png otherwise it will not work.

syntax like:sketch.png



import cv2
image=cv2.imread("captain_Amreica.png")
grey_img=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
invert=cv2.bitwise_not(grey_img)
blur=cv2.GaussianBlur(invert,(21,21),0)
inverted_blur=cv2.bitwise_not(blur)
sketch=cv2.divide(grey_img,inverted_blur,scale=256.0)
cv2.imwrite("sketch.png",sketch)

watch the Python Projects – Convert Images Into Pencil Sketch Using Python (5 lines)


RELATED ARTICLES

4 COMMENTS

  1. I do not know if it’s just me or if perhaps everyone else experiencing
    issues with your site. It appears as if some of the text in your content are running off the screen. Can somebody else please provide feedback and let
    me know if this is happening to them too? This might
    be a issue with my web browser because I’ve had this happen before.
    Many thanks

LEAVE A REPLY

Please enter your comment!
Please enter your name here

15,000FansLike
5,000FollowersFollow
535FollowersFollow
- Advertisment -spot_img

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Most Popular