Create Fun Cartoon Effects for Your Photos with OpenCV and Python
Want to transform your photos into fun, artistic cartoons? With the OpenCV library and a little Python code, it‘s easier than you might think! In this step-by-step guide, we‘ll walk through the process of applying cartoon effects to any image using OpenCV.
Whether you want to create unique social media posts, design eye-catching visuals, or just play around with creative effects, learning to cartoonize photos is a great skill to have. Let‘s jump in and see how it‘s done!
What is OpenCV?
First off, let‘s talk about what OpenCV is and why it‘s so powerful for image processing tasks like this. OpenCV, which stands for Open Source Computer Vision Library, is an open source library of programming functions for real-time computer vision applications.
Developed by Intel and first released in 2000, OpenCV is cross-platform and can be used with C++, Python, and Java across Windows, Linux, Mac OS, iOS, and Android. Best of all, it‘s free for both academic and commercial use.
Some of the key capabilities OpenCV provides include:
- Image and video input/output, display, and processing
- Object, face, text, and activity detection and recognition
- Feature detection and description for object tracking and 3D scene reconstruction
- Camera calibration and 3D vision from stereo cameras
- Machine learning algorithms for data clustering, classification, and regression
With over 2500 optimized algorithms, extensive documentation, and a large community of users and developers, OpenCV makes it possible to quickly and efficiently develop sophisticated computer vision applications. And Python‘s simple syntax and wide selection of libraries makes it an ideal language for experimenting with OpenCV.
Step 1: Edge Detection
Alright, let‘s start cartoonizing! The first step is to identify the edges and contours in our source photo that will form the basis of our cartoon effect. Edge detection allows us to find boundaries between objects in an image based on differences in brightness.
OpenCV provides several edge detection algorithms we can use like Sobel, Scharr, Laplacian, and Canny. For our purpose, the Canny edge detector will work well. Here‘s the code to perform edge detection with Canny:
import cv2
import numpy as np
img = cv2.imread("input.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200)
We start by importing the OpenCV and NumPy libraries. Then we load our source image using cv2.imread() and convert it to grayscale with cv2.cvtColor() since Canny operates on grayscale images.
Finally, we apply the Canny edge detector with a lower and upper threshold. These thresholds control which edges are considered significant based on the strength of brightness gradients. The optimal values depend on your particular image but 100 and 200 are good starting points.
The edges image will be a binary mask showing the detected edges in white on a black background, as seen below:

Step 2: Color Quantization
Color quantization is the process of reducing the total number of colors in an image. Since cartoons typically have a much smaller color palette than photographs, this step will help achieve that characteristic flat, posterized look.
To perform color quantization, we‘ll use OpenCV‘s K-means clustering algorithm. K-means aims to partition a set of data points into K clusters, where each point belongs to the cluster with the nearest mean.
Here‘s how we can apply K-means to the colors in our image:
Z = img.reshape((-1,3))
Z = np.float32(Z)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 8
ret, label, center = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)
quantized = center[label.flatten()]
quantized = quantized.reshape(img.shape)
First we reshape the image into a Nx3 array of RGB pixels to pass to K-means. We specify the criteria for the algorithm to stop – either reaching a max number of iterations (10) or a desired accuracy (1.0).
The key parameter is K which determines the number of colors in the result. A smaller value like 8 will posterize the image more while a larger value like 32 will retain more detail. After running K-means, we get a result where each pixel in the image is assigned to one of the K color clusters.
Converting the cluster centers to uint8 and reshaping the labels back into the dimensions of our original image produces the quantized result:

Step 3: Bilateral Filtering
To reduce some of the sharpness and increase the cartoon effect, we‘ll apply a bilateral filter to our color quantized image. Bilateral filtering smooths flat regions while preserving hard edges.
It accomplishes this by not only considering the spatial distance between pixels (like a Gaussian blur) but also the difference in intensity. Pixels are averaged with nearby pixels of similar intensity within a specified neighborhood diameter.
Here‘s how to apply a bilateral filter with OpenCV:
filtered = cv2.bilateralFilter(quantized, 9, 200, 200)
The first parameter is the quantized image from the previous step. 9 specifies the diameter of the pixel neighborhood to consider. 200 and 200 are intensity parameters that control the upper boundaries for measuring pixel similarity in the color space.
Applying the bilateral filter produces a result with flatter regions and preserved edges, enhancing the cartoon effect:

Step 4: Combine Edges and Colors
The final step is to combine our edge mask from step 1 with the color filtered image from step 3 to get our finished cartoon effect. We‘ll do this using a bitwise AND operation between the two images.
Bitwise AND will keep the edge pixels that are white in the mask while coloring them using the corresponding pixel colors from the filtered image. Here‘s the code:
cartoon = cv2.bitwise_and(filtered, filtered, mask=edges)
And voilà! We have our final cartoonized image:

Play around with the edge detection thresholds, number of color clusters (K), and bilateral filter parameters to fine tune the effect to your liking. Here‘s another example with some different settings:

Applications and Learning Resources
So what can you do with cartoon effect images? Some potential applications include:
- Social media posts and profile pictures
- Custom graphics and illustrations for blogs, videos, ads, etc.
- Smartphone app filters (like Prisma)
- Artistic projects and experimentation
The cartoon effect is just one example of the many creative possibilities OpenCV opens up. To learn more, check out the official OpenCV Python tutorials which cover everything from the basics of image and video processing to object detection, face recognition, feature matching, machine learning, and more.
LearnOpenCV is another great site with tons of tutorials and sample code to help you get started. And the PyImageSearch blog has an article on creating cartoon-style images and videos you may find helpful too.
I hope this guide has given you a taste of what you can do with OpenCV and Python and inspires you to create your own fun photo and video effects. The possibilities are endless – so dive in and happy coding!