Making a beautiful sketch requires a lot of time and effort. Luckily, programming has evolved so you can now easily transform images and create beautiful designs. One impressive technique you can carry out is to convert an image into a digital sketch.
While a mechanical sketch won’t look exactly like your own pencil drawing, the output is still worth experimenting with. Learn how to program such an app using the OpenCV module in the super-friendly Python language.
The OpenCV Module
OpenCV is an Intel-developed and maintained open-source library of computer vision methods used for image processing and computer vision. It makes it simple for users to create optimized applications, interact with images and videos in real time, and select the ideal algorithm for their needs.
Some of the popular applications of OpenCV include face detection, automatic face tracking in cinematography, social media filters, car license plate recognition, and CCTV monitoring. To use the OpenCV module in Python, open your terminal and type the following command:
pip install opencv-python
How to Convert Any Image Into a Sketch Using Python
To convert your favorite image into a digital sketch, start by placing it in the same folder as a new Python program, for easy referencing. Then start building your program using the following steps.
This example code is available in a GitHub repository and is free for you to use under the MIT License.
The first step is to import the OpenCV module into your environment. Once OpenCV is available, you can use its functionality to carry out various tasks. Pass the path of your image file into the imread() function to load it. Store your image in a variable—called image1 here—for future reference.
Store the title of the window in a variable named window_name. This will be useful when you choose to display the image using the imshow() function. This function requires two parameters: the title and the image you want to display.
import cv2
image1 = cv2.imread('image.jpg')
window_name = 'Actual image'
cv2.imshow(window_name, image1)
Once you have the desired image, you need to perform five operations to transform it into a sketch. First, convert the color image into grayscale. You can do so with the cvtColor() function. This function takes in the image whose colors you want to alter and a conversion code such as COLOR_BGR2GRAY.
grey_img = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
Once you have a grayscale image, invert its colors. At this point, you need to understand how a computer forms an image. An image consists of many tiny pixels with varying intensities. In a color image, each pixel contains red, green, and blue components, each with an intensity that varies from 0 to 255.
In a grayscale image, there are only shades of grey, so the intensity of a pixel varies between 0 and 1. To invert the pixel intensities of this image, pass the grayscale image to the bitwise_not() function.
As the name suggests, this function inverts the value of each pixel to its complementary equivalent. All pixels that are greater than 0 are set to 0, and all pixels that are equal to 0 are set to 255.
invert = cv2.bitwise_not(grey_img)
After inverting the pixel intensities, you can smooth the image with the help of Gaussian Blur. The Gaussian Blur process makes use of a Gaussian filter. A Gaussian filter is a low-pass filter that only allows low frequencies to pass through, removing the high-frequency component of a signal or an image.
OpenCV's GaussianBlur() function accepts four parameters. These are a matrix object that acts as the source image, the ksize (size of the kernel), and sigmaX (the Gaussian kernel standard deviation).
Suppose you have a physical photograph in your hand. If you wanted to blur it, you could apply pieces of wax or parchment paper over it. You can imagine the kernel as this transparent bit of paper. Digitally, this happens a bit differently. To blur, sharpen, and apply other effects on a digital image, you multiply a matrix with the intensity of the image's pixels.
The ksize is always a positive odd number. As you increase the kernel size, the blur increases. To understand sigmaX assume you are applying wax on a paper. As you apply wax, the paper becomes uniformly translucent. Similarly, you must keep the kernel's values close to a specific point (the average). The sigmaX value defines the difference between the average and other values of the pixels in an image.
Pass the inverted image, the kernel size as (21, 21) and 0 standard deviation to the Gaussian Blur function:
blur = cv2.GaussianBlur(invert, (21, 21), 0)
Pass the blurred image to the bitwise_not() function again to invert it:
invertedblur = cv2.bitwise_not(blur)
Finally, use the divide() function and to perform per-element division of the grayscale image array and the inverted blur image array with a scale of 256.
sketch = cv2.divide(grey_img, invertedblur, scale=256.0)
Essentially the function performs the following operation:
def divide(grey_img, b, invertedblur=256.0):
return (grey_img * scale) / invertedblur
Store the result in a variable named sketch. To save the final image, pass a name for your output file and the sketch image to the imwrite() function. To verify it, you can use the imread() function to load the saved sketch image, give a window title and display it using the imshow() function.
Use the waitkey() function by passing 0 to display the original image window and the generated sketch window until you press any key.
cv2.imwrite("sketch.jpeg", sketch)
image = cv2.imread("sketch.jpeg")
window_name ='Sketch image'
cv2.imshow(window_name, image)
cv2.waitKey(0)
Bring all the code together, and you have your program ready.
Sample Output of Converting an Image Into a Sketch Using This Python Program
You can choose a beautiful landscape image and run it through the program to generate this stunning digital sketch.
On a portrait image, the program generates the following digital sketch.
You can experiment with the parameters of the function according to your liking to generate your desired digital sketch.
Image Processing and Computer Vision
Image processing and computer vision are two closely related fields of technology. They both involve altering digital images to get desired outcomes. Image processing focuses on improving a picture, while computer vision looks to find patterns and objects within an image to understand it.
Scikit-image is another Python library that provides a wide variety of image-processing functions. It has several precompiled modules, filters, layers, transformations, and more. If you are looking for a module to use for deep learning models such as CNN and RNN you might want to explore Torchvision.