Best way to convert a "cartoon" image from JPG to PNG?

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Post Reply
MelB
Posts: 4
Joined: 2015-03-24T22:48:58-07:00
Authentication code: 6789

Best way to convert a "cartoon" image from JPG to PNG?

Post by MelB »

Hi all,

I've been a fan of this forum for a very long time and decided to register and ask for your wisdom on the problem that's been bugging me for a while.

Quite often I come across some "cartoon" images (images that used to contain very few unique colors) that someone has (needlessly) compressed into jpegs and now the image has artifacts.

For example, take this image:
Image

You can notice speckling around lines and the colors around lines are now blended as well. The original only had 6 colors and now the JPG has thousands.

Using something as simple as:

Code: Select all

convert input.jpg -dither None -colors 16 output.png
Produces the following image:

Image

If you use less colors, the white in eyes will not show up (they will be blue) because IM doesn't seem to have an option to restrict palette to only the most frequent colors in an image. The background blue color was also shifted. There are also problems with speckling around lines that are quite obvious when you zoom in:

Image

and there is a "double border" around some lines as can be seen in the following crop (notice the dark blue line below black line):

Image

How would you go about restoring this image and converting it to a small PNG so that all these problems are minimized?

Thank you so much for any insights and thank you for all the recipes that always get posted on here!
Last edited by MelB on 2015-03-26T15:49:16-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Best way to convert a "cartoon" image from JPG to PNG?

Post by snibgo »

We can find the most common colours, for example the 20 most common:

Code: Select all

f:\web\im>%IM%convert frog.jpg -format %c histogram:info:- |sort -r |head -n 20

     22049: (211,215,240) #D3D7F0 srgb(211,215,240)
     16735: ( 92,142, 55) #5C8E37 srgb(92,142,55)
     11143: ( 93,143, 56) #5D8F38 srgb(93,143,56)
      6741: ( 94,144, 57) #5E9039 srgb(94,144,57)
      3593: (  2, 71,254) #0247FE srgb(2,71,254)
      3469: (  3, 69,253) #0345FD srgb(3,69,253)
      1429: ( 95,145, 58) #5F913A srgb(95,145,58)
      1355: ( 94,142, 56) #5E8E38 srgb(94,142,56)
      1228: ( 94,144, 59) #5E903B srgb(94,144,59)
      1181: (214,216,241) #D6D8F1 srgb(214,216,241)
      1131: ( 93,141, 55) #5D8D37 srgb(93,141,55)
      1114: (  0,  0,  0) #000000 black
      1094: (255,255,255) #FFFFFF white
       973: (210,214,239) #D2D6EF srgb(210,214,239)
       958: ( 95,143, 57) #5F8F39 srgb(95,143,57)
       803: (213,215,240) #D5D7F0 srgb(213,215,240)
       698: ( 95,143, 59) #5F8F3B srgb(95,143,59)
       649: ( 94,144, 55) #5E9037 srgb(94,144,55)
Then a script needs to figure out that many of these entries are (almost) the same colour. Lines 2, 3, 4, 7, 8 and 9 are almost the same, as are 5 and 6.

After doing this grouping, the required colours are (probably) obvious.
snibgo's IM pages: im.snibgo.com
MelB
Posts: 4
Joined: 2015-03-24T22:48:58-07:00
Authentication code: 6789

Re: Best way to convert a "cartoon" image from JPG to PNG?

Post by MelB »

snibgo wrote:Then a script needs to figure out that many of these entries are (almost) the same colour. Lines 2, 3, 4, 7, 8 and 9 are almost the same, as are 5 and 6.

After doing this grouping, the required colours are (probably) obvious.
Well, that's where the complexity lies and none of it is really obvious (to me anyway) :)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Best way to convert a "cartoon" image from JPG to PNG?

Post by snibgo »

It looks to me that the original (pre-JPEG) was antialiased, so contained many more than 6 colours. So reducing to about 100 colours followed by "-statistic median 3x3" seems (to me) to be a good result.


If you want to reduce to the main colours only, which would eliminate all antialiasing, you could:

1. Find the main colours.
2. Remap ("-remap") the image to those colours.

Step (1) is the tricky part. How do we define "main colours"? One method is to isolate those areas of frog.jpg that contain unvarying colour. I find areas that contain detail and paint them out (make them transparent). Then I fill in (aka "inpaint") those transparent areas from adjacent areas.

Code: Select all

call %PICTBAT%slopeMag frog.jpg asm.png

%IMDEV%convert ^
  asm.png ^
  -threshold 1%% ^
  -negate ^
  frog.jpg ^
  +swap ^
  -compose CopyOpacity -composite ^
  asmt.png

call %PICTBAT%shiftFill asmt.png . asmtf.png
asmtf.png contains many areas of flat colour. We could isolate the largest ones with "-connected-components", but "-colors" works fairly well. It does make two slightly different versions of green.

Code: Select all

%IM%convert asmtf.png +dither -colors 10 a1.png

%IM%convert ^
  frog.jpg ^
  +dither ^
  -remap a1.png ^
  -statistic median 3x3 ^
  fm.png
In frog.jpg, some isolated background pixels are closer to white than to the background colour. "-statistic median 3x3" cures this.
Image

fm.png is the result. It is about 4% RMSE from frog.jpg, which is typical for this type of operation.
snibgo's IM pages: im.snibgo.com
MelB
Posts: 4
Joined: 2015-03-24T22:48:58-07:00
Authentication code: 6789

Re: Best way to convert a "cartoon" image from JPG to PNG?

Post by MelB »

That looks really decent! Thank you! I'll play some more with options. I've also been playing with posterize but haven't nailed it right just yet. I'll post an update soon.
MelB
Posts: 4
Joined: 2015-03-24T22:48:58-07:00
Authentication code: 6789

Re: Best way to convert a "cartoon" image from JPG to PNG?

Post by MelB »

snibgo, here's what I came up with today after 5+ hrs of reading (and coding) about artifact removal, jpeg compression and vectorization algorithms. It's all still a bunch of messy Matlab/Octave code (I prefer it for quick 'messing around' with image coding).

I use IM for decompression/compression but not much else. Some of the steps could be converted to IM commands.

Image

It's still not perfect. You can see a bunch of jagged little lines where the lines should be smooth. The color count is also increased from optimal (it's an 8bit image). That's due to anti-aliasing of lines but the colors are, overall, more uniform.

Here is the diff of pic above with the original jpg:

Image

I'll play around some more when I get some free time in few weeks..
joeyhoer
Posts: 1
Joined: 2016-01-20T16:17:18-07:00
Authentication code: 1151

Re: Best way to convert a "cartoon" image from JPG to PNG?

Post by joeyhoer »

Any chance you'd be willing to share your script?
Post Reply