Cartoons are something that everyone likes whether you are a child of 10 years or an adult of 40 years. What if I tell you, it’s possible to cartoonify an image of yours all by yourself by just writing a few lines of python code, wouldn’t it be awesome.

Let’s get started.

## Cartoonify an Image Python-Project

#### Requirements:

Basic knowledge of python(go through Learn Python Tutorial) . We can use any ide ( for example:  https://code.visualstudio.com/download , https://jupyter.org/).

You can go through this tutorial

https://studygyaan.com/python/blurring-an-image-using-opencv-python .

#### Modules:

OpenCV : It is a python open-source library, we use it for all sorts of image and video analysis, for instance, facial recognition and detection, photo editing and many other stunning applications.

NumPy : NumPy (an acronym for “Numeric Python” or “Numerical Python”) is a library for the Python programming language. It is used for working with large ,multi-dimensional arrays and matrices.

#### Installation:

Firstly, we have to open command prompt in administrator mode.

OpenCV: To install OpenCV we have to type the below command in the terminal.

`pip install opencv-python`

NumPy :Similarly, to install Numpy we will type the below command in the terminal.

`pip install numpy`

#### Approach:

1. Firstly, we have to import our module.

`import cv2`

2. Secondly, after reading the image we will downsampling it and then will use repetitive bilateral filter to reduce the color palette of the selected image.

``` num_downsample = 2
num_bilateralfilter = 7
ImgColor = ImgRgb
for i in range(num_downsample):
ImgColor = cv2.pyrDown(ImgColor)
for i in range(num_bilateralfilter):
ImgColor = cv2.bilateralFilter(ImgColor, d=9, sigmaColor=9, sigmaSpace=7) ```

3. Thirdly, we have to upsample the image to the original size.

``` for i in range(num_downsample):
ImgColor = cv2.pyrUp(ImgColor) ```

4. Fourthly, we will convert the image to grayscale and apply median blur to reduce noise.

``` ImgGray = cv2.cvtColor(ImgRgb, cv2.COLOR_RGB2GRAY)
ImgBlur = cv2.medianBlur(ImgGray, 7) ```

5. Then, we have to use adaptive thresholding to create an edge mask and detect and enhance the edges.

`ImgEdge = cv2.adaptiveThreshold(ImgBlur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blockSize=9, C=2)`

6. Finally, we will combine the color image and the edge mask and display the final image.

``` ImgEdge = cv2.cvtColor(ImgEdge, cv2.COLOR_GRAY2RGB)
ImgCartoon = cv2.bitwise_and(ImgColor, ImgEdge)
cv2.imshow("MyCartoon", ImgCartoon) ```

#### Source Code:

``` import cv2
num_downsample = 2
num_bilateralfilter = 7
ImgColor = ImgRgb
for i in range(num_downsample):
ImgColor = cv2.pyrDown(ImgColor)
for i in range(num_bilateralfilter):
ImgColor = cv2.bilateralFilter(ImgColor, d=9, sigmaColor=9, sigmaSpace=7)
for i in range(num_downsample):
ImgColor = cv2.pyrUp(ImgColor)
ImgGray = cv2.cvtColor(ImgRgb, cv2.COLOR_RGB2GRAY)
ImgBlur = cv2.medianBlur(ImgGray, 7)