Turning Photos into Cartoons

A Basic Tutorial for Image Modifications

My motivation for writing this post was the idea of building a custom One Piece poster, where my photo would appear, but in a cartoon version.

For those who don’t know, One Piece is a Japanese anime, made by Eiichiro Oda. This work lasts more than 20 years and it is not over yet. But particularly I’m in love with this anime, the anime deals with the pirate world and a group of fellows in search of One Piece besides many other very difficult themes and themes about human relations. But, Oda manages to do this in a very subtle and joyful way. If you’re curious, watch!

I looked for some face change apps, but I didn’t like any. Then I have an idea: make a manipulation of an image I have in python!

So I’m going to show you what I implemented in python and the end result of the whole process, I hope you like this post.

The first step is to load the image. For this we are going to use the cv2 library. And, with the combination of skimage and matplotlib libraries, check out the step-by-step construction of this new image.

Before showing the script, the folder I’m working on is arranged as follows:

For more details:

Starting with loading the libraries:

# packagesimport cv2import matplotlib.pyplot as pltfrom skimage import io

Upload image:

# upload image# import matplotlib.image as mpimg# file = mpimg.imread('images/image.jpg')img = cv2.imread("images/image.jpg")io.imshow(img)plt.show()

I chose the image below:

The next step will be to emphasize how to consume the image, normally this is seen a lot in cartoons, so let’s start doing that, with the following commands:

df = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)df_blur = cv2.medianBlur(df, 3)

Let’s understand each command, when we load the image it comes in RGB tones, normally to manipulate an image with better performance (when we don’t need to analyze by color), we use grayscale with the cv2.cvtColor command and the choice of COLOR_BGR2GRAY. Hence, the second step, after transforming the entire image to grayscale, it was to reduce the noise of that image to gray using cv2.medianBlur. When larger, less noise in black tones. Then the next step was to apply the adaptiveThreshold function to define the size of the edge line, the greater the more emphasis.

This is a step that requires experimentation, the more details in the image, I suggest a smaller number. In order to see the result of the image, I made the following commands:



edges = cv2.adaptiveThreshold(df_blur, 250, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 75, 3)io.imshow(edges)plt.show()

Reduction of the image cores palette.

Have you ever noticed that in a cartoon there is a more “finite” or smaller color palette than in photos?

A photo and a drawing are distinguished by the amount of colors they have. So, in this step, we’re going to quantize the use of the photo’s colors to get closer to a drawing. For this, we apply the K-Means clustering algorithm.

To do this, I use an fc_quant_color function that is found in the .py functions file in the utils subfolder.

# source functionexec(open('utils/functions.py').read())# quant colorimg = fc_quant_color(img, 7)io.imshow(img)plt.show()

Reducing noise with a filter, that is, we will blur the image so it doesn’t look so real.

SUM(Mask + Color-processed image)

cartoon = cv2.bitwise_and(blurred_img, blurred_img, mask=edges)io.imshow(cartoon)plt.show()io.imshow(cartoon)plt.savefig('cartoon/result.png')

Now that I have my cartoon-like image, a next step was to insert it into my wanted poster.

Well, I hope you enjoyed it. And thanks for everything!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andressa Contarato

I’m a statistician, with a postgraduate degree in Finance from UF, specializing in Data Science from DSA, MBA in Data Science and MBA in Project Management USP.