Coders Packet

Converting an Image in to a Pencil Sketch view by Python using OpenCV Library


This code can convert your photo to pencil sketched quickly. Just upload your photo, set the pencil shadow or thickness then you can view the sketched image.


In Python, a picture is just a double-dimensional array of integer values. So anyone can do a couple of matrix manipulations using different python libraries in order to get few interesting effects. In order to convert the general image into a pencil sketch, we will modify its true RGB values and assign its RGB values which equal to grey, in this way an input image as a sketch will be generated. A picture is basically an array of numeric values to Python. So we can perform different types of matrix manipulations to get some fascinating results. This code will display how to convert an image into a ‘pencil’ outline view.


  • Import all required modules.
  • Read the input image as a grayscale image
  • Remove any noise using a gaussian filter
  • Create a negative image (invert the image)
  • Dodge these two images(gray and inverted images). These two images reinforce some edges and complement some edges. An alternative, these two pictures can be blend with different weights.
  • Show finale image output using cv2.imwrite() and wait for a keystroke to close the display.



Download Complete Code


No comments yet