Cartoonization with Python Script

Tanvi Agarwal
Analytics Vidhya
Published in
4 min readSep 4, 2020

Machine Learning or should I call it “Magical Learning”? Each day I can feel the essence of ML and AI in our day to day life. ML is spreading its presence everywhere be it education industry, virtual bots or even in editing of images as I found an amazing project yesterday with the motive of Cartoonization of Image without any editing tools (heavy and costly)rather using small python script (easy and free).

In this article I am going to elaborate how an image is cartoonized at basic level with just two libraries of python namely, cv2 and numpy. For getting cartoon effect two functions are used:

  1. Bilateral Filter : This is used for smoothening of images and reducing noise as a result to reduce the color palette of an image to be cartoonized.
  2. Edge Detection : This function is applied to the resulting image to generate bold silhouettes.

Let us follow step wise coding to have some fun with your favourite image:

  1. Importing Necessary Libraries: The very first library to be imported is cv2 and then numpy.
Code to Import Libraries

2. Reading an Image for Operation: With the help of cv2.imread() function we will read an image from our local directory.

Reading an Image for performing Cartoonization

3. Resizing Image: Since bilateralFilter() function comes with a drawback of high computational cost we will resize the image to convert it into a low resolution image by reducing its width and height.

Resizing

4. Applying a Bilateral Filter: Resizing may result in aliasing of image pixels so we will use Gaussian pyramid to downscale the reduced image. But after this also, bilateral filter may process slowly, another tip is to iterate small bilateral filter to image rather than using large bilateral filter at once.

Bilateral Function

The arguments used in this function are:

  • d: Diameter of each pixel neighborhood.
  • sigmaColor: Value of sigma in the color space. More the value more are the chances of farther colors to get mixed.
  • sigmaSpace: Value of sigma in the coordinate space. More the value, more are the chances of farther pixels getting mixed, given that their colors lie within the sigmaColor range.

Restoring image to its original size by upscaling.

5. Converting image to Grayscale image: In this step, RGB image is converted to its grayscale version.

GrayScale Image

6. Applying Median Blur to Grayscale image: Median Blur is used to set median values of each pixel in gray scale image to reduce noise and have an efficient result to perform adaptive thresholding. Median blur is applied with a seven-pixel neighborhood.

Median Blur

7. Using Adaptive Thresholding: adaptiveThreshold() function is similar to threshold function used to convert grayscale image to binary image, as a cartoon image comes with few colors.

Adaptive Thresholding

8. Combining Color Image with Final Edge Image: Following so far with each step, here is the last step to get the final output by combining the original RGB image generated by bilateral filter and the edge masking using bitwise_and.

bitwise_and

Finally use imshow() function to look at the output.

Image Output

Few images on which I tried this script, are:

Final Result
Output

I hope you followed each step carefully and here is the link to github repo if you missed some step or stuck somewhere : https://github.com/tanviagwl98/ImageCartoonization_OpenCV

References :

--

--

Tanvi Agarwal
Analytics Vidhya

A techie gal with a writer's heart, just trying to help everyone as I learn, explore nd share.