niedziela, 16 stycznia 2011

Cartoonization in GIMP

Hi, I'm pasting a few pictures I cartoonized in GIMP from my own photos.






The images above were created by dividing a photograph into a few sections, each with unique hue. Then each section was cartoonized by reducing the number of colors, colorizing and finally applying the artistic/cartoon filter.

Here's a tutorial on how to do it. I know its impossible to describe it clearly without a video, so If you don't know how to do something, either ask here in a comment or find an appropriate tutorial on YouTube.

- If your photo is in a format that does not support transparency channel, for example jpg or bmp (unless its 32bit bmp), you will have to do seemingly useless action: duplicate the current layer and then delete the original one. This way, you will get an alpha channel for you photo.
- Apply unspotting filter on your image (Filters/Enhance/Despeckle). Even if you have only a bit of noise on the image, this will make selection by color much faster and remove unwanted black dots from final result of cartoonization. Which settings you should choose? You have to see the preview and make sure noise is removed and the existence of edges and important details is preserved.
- Think about what hues exist on the image. For example in case of the cat above there is green (for the grass) and orange (for the cat), as well as brown (for the fence).
- Think about the best way to select the area with given color. There is no robust way to select areas on any possible image. You have to find one that takes fairly short time. If it takes more than about 25 minutes, than there is a faster way, so don't bother going on and rethink you tactics. For example if a border between areas is fairly short, "Intelligent Scissors" will do fine. If there are, however, multiple inclusions and crossings or blurry areas, the length of the border may be enormous and then you would rather do "Select By Color". Look at the options for this tool. Think what is the feature of colors that differentiate your areas most. It will probably be hue ("Hue" in GIMP), brightness ("Value" in GIMP), or all color data in composite ("Composite" in GIMP). Don't be affraid to change the default "Composite" option. Experiment with the degree of similarity, so that the selection does not select too much on the image. Remember that you can add selections by holding SHIFT or subtract by holding CTRL key. Combine this with "Select By Color" to create a powerful selection tool for the whole image. Try to add selections so that no unwanted pixels end up selected. If it happens, undo and try with different settings. Observe the current selection. If you see that from now you can finish selecting by, for example, extending current selection by 1-2 pixels or manually adding or removing an area, do it, otherwise continue selecting by color.
- When you are done selecting, do Select/Float and then in the Layers tab right click on the Floating Selection and make a new layer out of it.
- If you have only 2 areas with distinct hue (as with one of the cat images above), then you are done dividing your image, otherwise extract all the other areas you need.
- Now is a good time to save your work as xcf file. This is a native GIMP format that contains whole history of changes, selections and layers so that you can open it
- For each area do the following:
A) In the Layers tab select the layer you want to work with (e.g. layer with an orange cat). Make other layers invisible by clicking the eye button in the Layers menu (for example do it so that cat is visible and grass is not).
B) Pick a representative color. To achieve this use the Color Picker Tool. In the tool options select "sample average" to make sure color represents average color of the current area, rather than an anomaly on the pixel you click on. Click on the current area on the image to pick the color (e.g. on a cat). It is NOT important to get the average brighness or intensity right, you just need the average hue. Keep this in mind when choosing point to pick color from.
C) Do color reduction (Colors/Posterize), selecting "5" on the slider (that doesn't actually reduce number of colors to 5, its a more involved maths, but you should see areas filled with consistent colors rather than gradients). Look at edges between colors on your image. If they look torn and clunky, undo and apply Gaussian Blur to the layer (Filters/Blur/Gaussian Blur). The radius of 4 pixels should be fine. Then do the color reduction. Your area should now be comprised of a few areas filled with unique color.
D) Turn your layer into shades of grey (Colors/Desaturation). As you do it, make sure you change the default "Lightness" to "Average". This will ensure that colors of the layer end up sufficiently distinct when turned to grey.
E) Now be careful because it is a very important stage. You need to colorize your layer. Do you remember picking a representative color? Now this color should be your Forgound Color in the color menu. Double click on it to open the color dialog. READ the hue. This is the number at the top right of the dialog, next to the top-most slider with "H" letter. Now close the color dialog and open Colors/Colorize. Set the "Hue" slider to the value you remember. Set the "Intensity" slider to maximum value. Then set the brightness to something sensible, so that the layer looks cool. Note that later you will be adding black cartoon-like lines, which will make image darker. Therefore you need a slightly bigger brightness than normal.
F) Increase contrast a bit (Colors/Brightness-Contrast) to make the layer look a bit more vivid.
E) Apply the Cartoon filter (Filters/Artistic/Cartoon). Good numbers to try are 5 for the "Mask Radius" and 0.7 for the "Percent Black", but you may need to experiment so that the black lines do not darken the color of the layer too much but are visible at the same time.

- Make all layers visible to observe the final result.
- Depending on how sharp were your selections and how much blur and unspotting you applied, you may see a strange, semi-transparent-looking borders between your areas. To get rid of them you may for example create a completely new black layer and put it below all your layers.
- Save/Export the image as you wish. If exporting as jpg, select quality very close to 100%, otherwise artefacts will be visible on the cartoon image.

Enjoy!