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.
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
Note: make sure your file extension name is png otherwise it will not work.
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)
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.
Say, you got a nice blog.Really looking forward to read more. Want more.
Woh I enjoy your articles, saved to favorites! .
Keep functioning ,terrific job!