snibgo's ImageMagick pages

Flat-colour cartoons

We replace each area of similar but varying colour with a single representative colour.

Scripts on this page assume that the version of ImageMagick in %IMDEV% has been built with various process modules. See Process modules.

Sample input

set SRC=barbara_test.png
barbara_test.pngjpg

Method: simple IM processes

See the official page Color reduction utilizing adaptive spatial subdivision.

We can apply a simple IM colour-reduction processes to either:

For example, we blur the input by the amount required to remove the detail, then apply one of the simple IM colour-reduction processes.

%IM%convert ^
  %SRC% ^
  -blur 0x4 ^
  +depth ^
  fcc_blr.png
fcc_blr.pngjpg
%IM%convert ^
  fcc_blr.png ^
  +dither ^
  -colors 28 ^
  fcc_cols.png

How many colours do we have?

%IM%convert ^
  fcc_cols.png ^
  -unique-colors ^
  -format %%w info: 
28
fcc_cols.png
%IM%convert ^
  fcc_blr.png ^
  +dither ^
  -posterize 5 ^
  fcc_post.png

How many colours do we have?

%IM%convert ^
  fcc_post.png ^
  -unique-colors ^
  -format %%w info: 
40
fcc_post.png
%IM%convert ^
  fcc_blr.png ^
  -segment 2000x1.5 ^
  +depth ^
  fcc_seg.png

How many colours do we have?

%IM%convert ^
  fcc_seg.png ^
  -unique-colors ^
  -format %%w info: 
37
fcc_seg.png

The "-colors" and "-segment" versions are quite good. However, setting parameters to capture the small yellow objects on the table also creates multiple thin strips at edges, defeating the objective of making areas of flat colour.

IM's colour-reduction process, when deciding which colours to use, doesn't consider edge pixels to be more or less important than non-edge pixels.

Method: from partition boundary mask

We use a partition boundary created on the Partition boundary masks page.

set PTBND=pb_seg8.png
pb_seg8.png

We can see the input image divided into parts:

%IM%convert ^
  %SRC% ^
  %PTBND% ^
  -compose Lighten -composite ^
  fcc_inparts.png
fcc_inparts.pngjpg

Each partition is a reasonably constant colour.

From a thick boundary mask, we make a thin boundary mask by thinning to an 8-connected skeleton and repeated pruning.

%IM%convert ^
  %PTBND% ^
  -morphology Thinning:-1 Skeleton  ^
  fcc_morphclsk.png

call %PICTBAT%pruneStubsRep ^
  fcc_morphclsk.png fcc_partbnd.png all
fcc_partbnd.png

Make a 4-connected version.

%IM%convert ^
  fcc_partbnd.png ^
  -morphology thicken "3>:-,0,-,1,-,1,0,1,0" ^
  -morphology thicken "3>:-,0,0,1,-,0,0,1,-" ^
  fcc_partbnd4.png
fcc_partbnd4.png

From the 4-connected skeleton partition boundary, we find the components.

%IM%convert ^
  fcc_partbnd4.png ^
  -define "connected-components:verbose=true" ^
  -connected-components 4 ^
  -auto-level ^
  fcc_gray.png 
Objects (id: bounding-box centroid area mean-color):
  1: 780x576+0+0 406.3,264.9 27903 gray(255)
  111: 230x451+256+125 366.8,268.8 27706 gray(0)
  270: 170x217+10+325 73.9,413.3 17674 gray(0)
  306: 131x164+646+412 718.1,502.0 15708 gray(0)
  287: 154x209+527+356 599.9,465.9 12149 gray(0)
  296: 198x191+84+385 184.9,502.3 10211 gray(0)
  299: 102x131+200+402 250.8,474.8 9575 gray(0)
  284: 171x223+350+351 415.5,490.0 9310 gray(0)
  6: 77x136+294+0 334.0,66.4 9010 gray(0)
  226: 62x326+269+250 312.0,426.0 8905 gray(0)
  7: 84x130+370+0 405.6,64.3 8747 gray(0)
  89: 147x85+11+104 80.5,142.9 8474 gray(0)
  281: 128x154+383+340 434.8,417.2 8177 gray(0)
  189: 233x123+95+211 218.7,277.8 8055 gray(0)
  240: 75x169+702+271 751.4,337.0 6884 gray(0)
  241: 117x154+638+272 704.4,362.9 6876 gray(0)
  282: 97x121+198+344 251.6,395.5 6040 gray(0)
  324: 152x55+12+517 94.1,545.5 5955 gray(0)
  244: 115x99+193+280 250.6,327.3 5537 gray(0)
  179: 145x120+35+204 116.1,259.4 5374 gray(0)
  21: 14x576+773+0 782.0,287.3 5191 gray(0)
  272: 112x182+534+326 578.2,401.5 5000 gray(0)
  153: 63x98+714+177 748.3,231.5 4767 gray(0)
  23: 72x108+87+4 122.4,56.3 4587 gray(0)
  309: 82x82+81+433 129.1,474.2 4487 gray(0)
  298: 87x164+607+394 651.1,484.7 4415 gray(0)
  228: 91x92+378+255 418.5,301.2 4370 gray(0)
  24: 114x66+178+4 228.5,30.0 4331 gray(0)
  48: 104x73+573+45 637.9,73.7 4116 gray(0)
  252: 87x156+445+305 493.0,402.7 4001 gray(0)
  70: 74x129+492+85 520.1,141.8 3637 gray(0)
  57: 107x58+164+63 219.7,88.8 3571 gray(0)
  2: 54x102+59+0 84.0,39.6 3468 gray(0)
  321: 76x69+332+503 371.9,545.4 3387 gray(0)
  145: 62x150+594+167 623.3,250.3 3344 gray(0)
  141: 197x54+111+158 220.8,187.2 3262 gray(0)
  85: 96x84+641+98 684.4,145.5 3196 gray(0)
  19: 86x130+668+0 698.4,54.4 3180 gray(0)
  232: 104x55+500+259 552.7,286.3 3169 gray(0)
  251: 77x115+441+300 488.3,350.9 3080 gray(0)
  60: 57x146+576+66 603.7,136.4 3026 gray(0)
  12: 102x42+499+0 545.2,20.8 3019 gray(0)
  293: 50x110+507+365 537.8,424.1 2810 gray(0)
  235: 135x62+57+261 127.7,294.5 2732 gray(0)
  249: 13x286+0+290 4.1,434.1 2639 gray(0)
  203: 64x67+411+219 443.2,251.3 2546 gray(0)
  264: 34x94+10+320 23.4,365.5 2419 gray(0)
  214: 81x43+50+229 92.9,250.0 2318 gray(0)
  39: 46x124+700+24 725.5,84.1 2301 gray(0)
  139: 59x58+56+157 85.9,182.2 2235 gray(0)
  26: 36x137+742+5 767.4,79.2 2219 gray(0)
  62: 22x219+0+71 5.2,193.9 2198 gray(0)
  17: 74x37+600+0 637.0,18.2 2168 gray(0)
  258: 84x50+556+311 591.4,330.6 2071 gray(0)
  64: 39x102+561+75 583.2,120.5 2010 gray(0)
  211: 52x77+278+226 305.1,262.6 1981 gray(0)
  44: 178x20+518+34 601.9,42.0 1842 gray(0)
  27: 49x109+42+6 59.8,59.3 1813 gray(0)
  84: 60x48+161+97 190.6,117.4 1791 gray(0)
  265: 36x128+510+320 526.1,381.9 1778 gray(0)
  22: 65x129+699+2 721.1,54.0 1675 gray(0)
  326: 90x38+195+521 233.7,536.3 1649 gray(0)
  222: 47x58+656+240 677.5,268.6 1641 gray(0)
  41: 31x79+467+29 480.0,63.4 1633 gray(0)
  186: 53x53+158+210 183.9,236.3 1606 gray(0)
  315: 74x47+467+473 499.6,496.8 1552 gray(0)
  123: 46x61+731+139 751.0,164.1 1492 gray(0)
  188: 67x60+188+211 221.8,238.7 1489 gray(0)
  301: 44x78+626+407 646.2,445.9 1485 gray(0)
  165: 41x63+671+191 692.3,219.8 1456 gray(0)
  68: 37x65+265+79 282.3,107.2 1415 gray(0)
  136: 35x82+590+149 607.2,189.8 1403 gray(0)
  34: 33x61+14+17 30.5,46.8 1365 gray(0)
  245: 62x54+669+286 698.8,311.1 1326 gray(0)
  38: 29x65+441+24 454.5,58.2 1305 gray(0)
  146: 32x76+700+167 714.6,195.9 1289 gray(0)
  318: 37x59+498+490 517.5,520.1 1254 gray(0)
  248: 71x35+558+288 593.1,304.6 1247 gray(0)
  337: 155x23+10+553 63.0,568.5 1225 gray(0)
  28: 26x72+0+7 9.5,42.6 1193 gray(0)
  199: 39x47+480+214 500.7,236.8 1180 gray(0)
  94: 28x75+142+114 156.4,155.8 1144 gray(0)
  50: 41x41+539+53 561.0,71.6 1141 gray(0)
  218: 57x36+534+236 561.7,254.4 1140 gray(0)
  113: 35x54+624+127 641.5,157.6 1138 gray(0)
  150: 47x39+532+177 557.7,197.9 1108 gray(0)
  51: 51x33+250+54 275.4,68.7 1107 gray(0)
  195: 60x29+30+213 61.2,225.1 1093 gray(0)
  316: 58x63+423+478 453.6,509.1 1043 gray(0)
  43: 66x33+236+30 269.5,48.9 1034 gray(0)
  259: 42x44+638+312 659.7,332.6 979 gray(0)
  104: 54x31+196+120 225.2,134.8 978 gray(0)
  175: 36x46+9+202 21.9,227.9 974 gray(0)
  332: 72x33+625+543 646.5,563.3 948 gray(0)
  59: 32x51+28+66 43.2,90.7 942 gray(0)
  236: 34x45+256+261 275.0,283.4 928 gray(0)
  40: 21x67+744+28 754.9,62.2 893 gray(0)
  173: 28x45+631+201 643.1,225.1 883 gray(0)
  9: 55x32+435+0 461.8,16.5 872 gray(0)
  291: 40x45+142+362 160.0,383.5 865 gray(0)
  297: 45x61+104+386 127.5,414.6 848 gray(0)
  254: 29x45+465+310 480.8,328.1 844 gray(0)
  310: 27x58+350+433 361.3,463.4 829 gray(0)
  312: 46x44+406+447 428.2,467.7 824 gray(0)
  49: 44x39+500+50 516.8,64.1 824 gray(0)
  33: 32x59+154+15 170.1,49.3 815 gray(0)
  56: 50x40+504+62 531.2,81.5 808 gray(0)
  290: 35x42+176+361 194.9,382.4 790 gray(0)
  317: 38x37+49+483 70.4,504.8 787 gray(0)
  187: 38x38+551+210 571.4,227.2 772 gray(0)
  144: 41x30+667+166 687.1,180.5 755 gray(0)
  273: 25x46+542+327 552.1,350.9 746 gray(0)
  277: 36x40+166+333 184.6,352.4 727 gray(0)
  101: 31x57+451+118 468.3,150.8 708 gray(0)
  107: 17x72+477+122 485.1,160.4 704 gray(0)
  18: 34x34+663+0 680.3,13.2 702 gray(0)
  292: 36x37+448+362 466.7,378.9 697 gray(0)
  11: 45x27+463+0 489.9,8.6 686 gray(0)
  169: 51x19+18+197 44.3,206.2 686 gray(0)
  239: 37x28+470+269 487.1,281.3 681 gray(0)
  247: 29x31+10+288 24.3,301.6 670 gray(0)
  54: 24x53+146+59 157.4,84.3 669 gray(0)
  63: 23x43+7+74 16.3,93.8 660 gray(0)
  335: 45x21+520+551 541.9,563.3 638 gray(0)
  307: 34x37+373+419 393.2,434.1 622 gray(0)
  285: 36x51+594+353 610.7,377.7 619 gray(0)
  221: 28x37+637+239 651.4,256.0 613 gray(0)
  130: 40x31+240+143 261.7,162.3 607 gray(0)
  25: 32x27+269+5 286.7,16.3 600 gray(0)
  178: 38x27+83+204 100.5,218.0 592 gray(0)
  134: 37x29+208+147 226.8,163.2 580 gray(0)
  166: 42x25+200+192 219.2,202.7 571 gray(0)
  303: 33x28+127+408 145.7,422.8 570 gray(0)
  280: 34x29+624+338 640.5,351.8 566 gray(0)
  336: 32x24+409+552 423.6,565.8 561 gray(0)
  325: 25x30+478+517 490.8,531.0 561 gray(0)
  142: 32x42+523+158 540.2,179.0 550 gray(0)
  73: 27x35+442+90 453.3,106.4 549 gray(0)
  333: 48x27+552+549 573.7,564.2 546 gray(0)
  230: 48x19+475+256 498.3,264.3 541 gray(0)
  276: 28x33+377+331 390.0,348.4 540 gray(0)
  110: 21x38+278+125 289.1,145.7 532 gray(0)
  138: 26x35+515+156 527.3,172.7 531 gray(0)
  140: 33x29+179+157 194.9,169.3 515 gray(0)
  96: 26x43+239+115 252.1,136.6 510 gray(0)
  133: 37x30+162+146 178.8,158.8 506 gray(0)
  98: 23x34+497+116 507.2,134.4 492 gray(0)
  4: 100x8+178+0 222.7,2.1 490 gray(0)
  183: 23x34+657+206 667.4,222.3 476 gray(0)
  0: 59x12+0+0 25.6,3.8 473 gray(0)
  29: 25x47+165+7 177.6,29.4 455 gray(0)
  223: 22x37+693+243 704.8,261.2 452 gray(0)
  131: 22x28+299+143 309.9,156.5 426 gray(0)
  155: 41x16+120+179 139.1,188.0 419 gray(0)
  225: 35x21+28+248 46.8,257.5 412 gray(0)
  242: 28x21+22+273 36.5,282.8 401 gray(0)
  327: 23x29+401+532 410.7,546.1 395 gray(0)
  162: 32x27+431+190 447.2,204.1 392 gray(0)
  206: 21x36+586+221 594.8,236.7 391 gray(0)
  338: 21x19+756+553 766.1,562.1 386 gray(0)
  100: 28x26+442+118 455.4,129.9 386 gray(0)
  207: 30x18+519+222 533.3,230.9 379 gray(0)
  72: 21x28+487+88 496.4,101.6 377 gray(0)
  220: 25x21+519+239 528.9,248.3 376 gray(0)
  15: 28x19+571+0 584.5,7.2 368 gray(0)
  32: 13x40+150+12 156.1,32.1 366 gray(0)
  151: 35x21+632+177 651.8,185.8 366 gray(0)
  176: 25x23+248+203 259.0,214.7 358 gray(0)
  229: 28x21+18+256 30.3,266.2 355 gray(0)
  217: 25x25+466+236 478.8,247.6 352 gray(0)
  88: 15x35+734+103 740.3,121.2 350 gray(0)
  81: 22x28+620+96 630.5,110.2 347 gray(0)
  115: 20x30+255+128 265.0,143.5 337 gray(0)
  3: 64x12+113+0 148.6,2.7 336 gray(0)
  117: 14x38+554+130 559.8,150.1 331 gray(0)
  157: 35x13+34+187 52.0,193.3 322 gray(0)
  143: 23x22+552+165 562.9,174.7 315 gray(0)
  156: 31x12+162+184 177.3,189.8 312 gray(0)
  106: 16x43+561+121 568.2,142.8 309 gray(0)
  334: 22x25+591+550 602.0,562.5 306 gray(0)
  99: 22x18+351+117 360.9,125.1 305 gray(0)
  237: 21x22+45+268 55.6,277.1 299 gray(0)
  95: 16x36+484+114 492.0,130.2 298 gray(0)
  102: 25x24+535+118 548.2,130.3 294 gray(0)
  47: 13x34+492+44 497.1,61.6 291 gray(0)
  295: 19x22+446+381 453.6,390.1 283 gray(0)
  61: 15x28+138+67 145.5,81.0 274 gray(0)
  339: 22x19+604+557 616.8,567.4 274 gray(0)
  128: 29x20+199+142 214.3,151.8 274 gray(0)
  257: 34x18+430+311 449.2,316.2 270 gray(0)
  283: 23x17+438+344 448.5,353.0 268 gray(0)
  255: 24x17+534+310 546.0,317.8 266 gray(0)
  262: 32x16+429+318 445.7,324.5 264 gray(0)
  328: 20x21+519+537 527.1,548.8 249 gray(0)
  103: 17x25+686+118 694.9,130.1 249 gray(0)
  86: 23x19+43+99 55.4,110.2 239 gray(0)
  246: 33x18+465+287 481.6,296.4 235 gray(0)
  319: 18x18+453+491 462.1,498.9 234 gray(0)
  46: 18x20+186+43 194.6,52.3 229 gray(0)
  196: 24x14+505+213 515.8,219.0 214 gray(0)
  148: 11x26+10+175 14.4,187.5 210 gray(0)
  114: 13x25+165+128 170.0,140.1 205 gray(0)
  8: 25x14+417+0 429.5,4.4 200 gray(0)
  116: 18x17+744+129 752.4,136.2 200 gray(0)
  260: 21x11+10+313 18.9,318.2 196 gray(0)
  16: 13x23+594+0 600.5,10.5 195 gray(0)
  42: 10x25+741+29 745.4,40.6 195 gray(0)
  87: 15x22+253+101 261.2,110.9 194 gray(0)
  30: 18x16+26+8 35.6,14.8 192 gray(0)
  209: 19x19+543+224 552.0,233.0 190 gray(0)
  343: 62x4+441+572 470.9,574.0 186 gray(0)
  52: 25x8+621+55 633.4,58.5 183 gray(0)
  224: 20x17+10+247 17.7,253.1 182 gray(0)
  5: 48x6+279+0 299.0,1.5 181 gray(0)
  212: 19x15+460+227 469.4,234.4 180 gray(0)
  97: 17x15+747+115 754.6,122.2 176 gray(0)
  341: 53x6+516+570 542.6,573.7 174 gray(0)
  238: 23x13+508+268 519.0,274.4 170 gray(0)
  288: 19x20+145+358 153.5,367.4 168 gray(0)
  77: 18x13+127+93 136.2,99.7 168 gray(0)
  243: 18x18+598+277 606.5,285.3 166 gray(0)
  250: 21x13+466+296 475.1,303.4 164 gray(0)
  167: 13x21+486+193 491.7,203.9 163 gray(0)
  121: 10x23+448+136 452.5,146.9 155 gray(0)
  263: 24x9+67+319 78.3,322.9 154 gray(0)
  74: 12x23+458+90 463.5,99.3 151 gray(0)
  171: 14x17+517+198 523.2,205.8 149 gray(0)
  216: 15x16+36+235 43.7,243.2 149 gray(0)
  45: 16x12+501+39 509.2,44.9 146 gray(0)
  125: 11x18+547+141 551.5,149.5 141 gray(0)
  174: 15x17+650+201 656.5,208.7 140 gray(0)
  215: 17x13+668+230 676.1,236.1 139 gray(0)
  152: 9x17+750+177 754.4,184.9 136 gray(0)
  202: 11x19+212+216 216.6,224.9 136 gray(0)
  289: 10x18+292+358 296.4,365.8 136 gray(0)
  149: 9x18+741+176 745.3,185.3 134 gray(0)
  161: 14x14+242+190 248.6,196.6 134 gray(0)
  193: 13x17+457+212 462.7,220.8 124 gray(0)
  79: 9x20+468+95 472.1,103.8 122 gray(0)
  185: 14x14+533+209 539.3,216.2 115 gray(0)
  213: 13x14+205+228 210.5,235.4 113 gray(0)
  180: 18x10+229+204 237.1,208.5 111 gray(0)
  168: 12x13+658+194 664.5,199.1 105 gray(0)
  147: 13x12+170+172 176.1,178.1 105 gray(0)
  109: 10x13+627+123 631.3,128.5 103 gray(0)
  304: 15x11+647+408 654.6,412.2 103 gray(0)
  58: 14x11+576+65 582.8,70.2 100 gray(0)
  71: 10x15+736+86 740.7,92.7 99 gray(0)
  90: 9x16+477+104 480.7,111.6 98 gray(0)
  164: 11x17+645+191 649.5,199.0 98 gray(0)
  191: 12x11+437+212 443.2,217.9 97 gray(0)
  331: 11x12+195+543 199.3,548.0 97 gray(0)
  205: 12x14+473+221 479.3,227.8 97 gray(0)
  67: 9x12+490+78 494.1,83.7 96 gray(0)
  53: 11x12+185+58 189.6,64.0 95 gray(0)
  344: 31x4+748+572 763.5,573.9 94 gray(0)
  329: 12x13+595+539 600.3,544.7 94 gray(0)
  119: 11x12+299+134 304.0,139.8 94 gray(0)
  200: 13x11+545+215 551.5,219.8 93 gray(0)
  55: 10x13+738+61 742.7,68.0 93 gray(0)
  75: 13x9+547+91 552.8,95.5 91 gray(0)
  340: 13x10+17+566 23.7,570.5 89 gray(0)
  37: 10x13+490+22 494.3,28.0 88 gray(0)
  66: 10x11+737+75 741.7,79.5 87 gray(0)
  69: 11x9+457+81 462.0,84.8 85 gray(0)
  36: 12x12+746+19 751.7,24.5 84 gray(0)
  308: 13x12+119+430 125.4,435.8 84 gray(0)
  20: 20x7+755+0 764.9,1.9 83 gray(0)
  330: 12x8+231+541 236.8,544.4 83 gray(0)
  268: 10x11+44+323 47.7,327.8 83 gray(0)
  181: 9x15+527+205 531.0,212.2 82 gray(0)
  10: 15x7+447+0 455.2,2.5 80 gray(0)
  233: 9x11+524+260 528.4,264.5 80 gray(0)
  124: 10x11+537+141 541.8,145.7 80 gray(0)
  160: 8x12+632+189 635.6,194.0 77 gray(0)
  177: 9x12+480+203 484.0,208.5 74 gray(0)
  83: 9x11+24+97 28.5,102.3 73 gray(0)
  159: 11x10+26+189 30.2,193.7 73 gray(0)
  120: 9x12+175+135 178.4,140.6 70 gray(0)
  204: 10x10+450+221 454.8,225.2 69 gray(0)
  302: 9x10+738+407 742.3,411.2 69 gray(0)
  118: 10x12+680+133 684.0,138.4 68 gray(0)
  279: 10x9+268+337 272.7,340.7 66 gray(0)
  198: 10x10+467+214 471.6,218.2 65 gray(0)
  323: 10x11+549+514 553.1,518.9 65 gray(0)
  112: 11x10+717+125 722.0,130.0 64 gray(0)
  313: 10x11+464+453 468.8,457.0 64 gray(0)
  314: 10x11+507+470 511.6,475.4 63 gray(0)
  266: 11x9+56+321 61.3,325.4 62 gray(0)
  182: 11x9+463+206 467.9,210.2 61 gray(0)
  269: 11x9+423+324 428.1,328.7 61 gray(0)
  172: 9x11+471+201 475.3,205.5 60 gray(0)
  278: 10x11+42+335 46.1,339.4 60 gray(0)
  163: 6x13+640+190 642.9,196.2 60 gray(0)
  192: 10x8+449+212 453.4,215.2 60 gray(0)
  190: 8x10+273+212 277.3,216.5 56 gray(0)
  275: 8x9+58+330 61.2,333.8 55 gray(0)
  31: 9x9+606+10 609.7,13.7 55 gray(0)
  105: 7x11+466+120 469.7,124.9 55 gray(0)
  129: 11x6+763+142 767.9,144.2 54 gray(0)
  234: 8x10+14+261 17.2,266.0 53 gray(0)
  271: 11x8+435+325 439.9,328.8 52 gray(0)
  256: 10x7+627+310 631.0,313.3 52 gray(0)
  274: 7x10+51+329 54.2,333.8 49 gray(0)
  267: 7x9+36+322 39.2,325.9 46 gray(0)
  261: 8x9+52+316 54.8,320.0 46 gray(0)
  342: 13x4+9+572 14.7,573.7 43 gray(0)
  231: 9x6+249+257 253.3,259.8 43 gray(0)
  305: 9x10+610+409 614.0,413.5 42 gray(0)
  320: 8x9+106+496 109.3,500.1 41 gray(0)
  219: 9x7+198+238 202.0,241.1 41 gray(0)
  286: 9x8+207+354 211.2,357.5 41 gray(0)
  135: 8x8+532+148 535.1,151.8 41 gray(0)
  91: 7x10+214+107 217.0,111.8 40 gray(0)
  170: 8x8+237+198 240.5,201.9 40 gray(0)
  311: 7x8+605+445 608.2,448.3 39 gray(0)
  197: 8x7+428+214 431.3,217.0 38 gray(0)
  253: 7x8+528+309 530.8,312.1 38 gray(0)
  35: 7x9+756+18 759.5,21.7 37 gray(0)
  127: 7x9+190+142 193.5,145.9 37 gray(0)
  194: 7x7+476+212 478.7,214.8 35 gray(0)
  137: 7x7+539+152 542.2,154.9 34 gray(0)
  65: 7x7+587+75 590.3,78.2 33 gray(0)
  76: 7x7+567+92 570.0,95.2 33 gray(0)
  158: 5x7+20+189 22.4,192.1 29 gray(0)
  132: 5x6+458+145 460.1,147.3 27 gray(0)
  80: 6x6+575+95 577.0,97.6 27 gray(0)
  78: 6x5+561+94 563.5,96.3 26 gray(0)
  208: 7x6+516+224 518.9,226.8 26 gray(0)
  154: 6x6+182+178 184.4,181.1 26 gray(0)
  92: 6x6+694+112 696.6,114.8 25 gray(0)
  345: 10x3+504+573 508.2,574.2 24 gray(0)
  122: 6x6+466+137 468.4,139.8 24 gray(0)
  93: 6x6+468+113 470.5,115.7 22 gray(0)
  14: 7x4+526+0 529.5,1.2 21 gray(0)
  322: 6x6+161+514 163.3,516.5 21 gray(0)
  126: 6x5+756+141 758.5,143.0 21 gray(0)
  227: 5x5+614+253 616.0,254.9 20 gray(0)
  184: 5x5+513+208 515.2,210.3 19 gray(0)
  108: 5x5+638+122 639.9,124.4 18 gray(0)
  300: 5x5+151+405 152.9,407.3 18 gray(0)
  210: 5x5+561+224 562.9,225.9 18 gray(0)
  201: 5x5+589+215 591.2,217.3 18 gray(0)
  294: 5x5+715+371 716.8,372.9 18 gray(0)
  13: 9x5+525+0 529.3,2.4 17 gray(255)
  82: 5x5+671+96 672.7,97.7 16 gray(0)
  346: 5x3+626+573 628.1,574.3 11 gray(0)
fcc_gray.png

Most components are black.

The scripts

The script skel2Parts.bat takes a 4-connected skeleton of a boundary mask, lists the components, reads this text to create an exclusion list of the components that were white, and creates an image of just the components that were black, with each component in a different shade of gray. (These shades may or may not contain black and/or white.)

It creates two versions of that image: one with transparency where the boundaries were, the other shift-filled.

It also creates a Nx1 pixel file containing the shades of gray.

call %PICTBAT%skel2Parts fcc_partbnd4.png fcc_parts.png

This has created image files:

%IM%identify fcc_parts_*.png 
fcc_parts_compsf.png PNG 787x576 787x576+0+0 16-bit sRGB 32.6KB 0.000u 0:00.000
fcc_parts_compst.png PNG 787x576 787x576+0+0 16-bit sRGB 48.8KB 0.000u 0:00.000
fcc_parts_grays.png PNG 345x1 345x1+0+0 16-bit sRGB 363B 0.000u 0:00.000

The _grays file contains one pixel per partition, so the width of this image is the number of partitions.

The script parts2FlatCols.bat uses those files to construct a flat-colour cartoon version of the original image. For each of the N pixels in the _grays file, it makes a mask from the _compsf file to isolate the pixels from the original, and scale these to a single pixel representing the mean colour of the partition. It appends these with the gray values into a 2xN image, _2xn.

Then it reads _2xn, to change the grays from _compsf into the corresponding mean colour of the partition.

The scaling to find the mean is done in RGB space. The result is lighter than when done in sRGB or Lab space. The case is arguable, but I think the RGB result is best.

call %PICTBAT%parts2FlatCols ^
  %SRC% fcc_parts.png fcc_flat.png

How many colours do we have?

%IM%convert ^
  fcc_flat.png ^
  -unique-colors ^
  -format %%w info: 
345
fcc_flat.png

The result is good. Despite the flat tones, most picture elements are recognisable.

We can use this image as the first approximation for a relaxation fill. The mask on this page has lines that are only one pixel thick. To have two colours meeting at an edge, we need lines at least two pixels thick. So, for the image that is to be filled, we use ctx_sh_edge.png from the Cartoon and texture page.

call %PICTBAT%relaxFillMS ^
  ctx_sh_edge.png fcc_flat.png ^
  fcc_rlx.png . 0.0001 50

if ERRORLEVEL 1 goto error

echo rfITER=%rfITER% 
rfITER=2550 
fcc_rlx.pngjpg

The result is more three-dimensional. This result is almost identical to ctx_rf3.png at Cartoon and texture: relaxation, which was made with no specified first approximation, bur required fewer iterations.

Blending

Blending the input image with the flat-colour cartoon gives pleasing results:

Not much flat-colour.

%IM%convert ^
  %SRC% ^
  fcc_flat.png ^
  -compose Blend ^
    -define compose:args=33 -composite ^
  +depth ^
  fcc_blnd0.png
fcc_blnd0.pngjpg

Mostly flat-colour.

%IM%convert ^
  %SRC% ^
  fcc_flat.png ^
  -compose Blend ^
    -define compose:args=67 -composite ^
  +depth ^
  fcc_blnd1.png
fcc_blnd1.pngjpg

Stained glass

We can easily create stained glass effects by compositing the flat-colour image with any of the white-line images.

%IM%convert ^
  fcc_flat.png ^
  ( ctx_edges.png ^
    -negate -transparent White ^
  ) ^
  -compose Over -composite ^
  fcc_sg1.png
fcc_sg1.png
%IM%convert ^
  fcc_flat.png ^
  ( fcc_partbnd.png ^
    -negate -transparent White ^
  ) ^
  -compose Over -composite ^
  fcc_sg2.png
fcc_sg2.png

Scripts

For convenience, .bat scripts are also available in a single zip file. See Zipped BAT files.

skel2Parts.bat

rem Given %1 is a white 4-connected skeleton enclosing black components,
rem %2 is output name
rem creates:
rem   _compst image where each component is different shade of gray, with transparent boundaries
rem   _compsf likewise, with boundaries filled (by shift right and down)
rem   _grays  Nx1 image of the gray colours

@if "%1"=="" findstr /B "rem @rem" %~f0 & exit /B 1

@setlocal enabledelayedexpansion

@call echoOffSave

call %PICTBAT%setInOut %1 s2p

if not "%2"=="" if not "%2"=="." set OUTFILE=%2

set FILE_FMT=%OUTFILE%

for /F %%F in ("%FILE_FMT%") do (
  set IMG_COMPST=%%~dpnF_compst%%~xF
  set IMG_COMPSF=%%~dpnF_compsf%%~xF
  set IMG_GRAYS=%%~dpnF_grays%%~xF
)

del %IMG_COMPST% >nul
del %IMG_COMPSF% >nul
del %IMG_GRAYS% >nul

set COMPLIST=s2p_complist.lis

%IM%convert ^
  %INFILE% ^
  -colorspace Gray ^
  -alpha off ^
  -negate ^
  -define "connected-components:verbose=true" ^
  -connected-components 4 ^
  NULL: >%COMPLIST%

if ERRORLEVEL 1 exit /B 1

set HAS_ANY=0
set CH_COLS=
set CH_EXCL=
set nFnd=0
set nExcl=0
for /F "skip=1 tokens=1-5 delims=: " %%A in (%COMPLIST%) do (
  set HAS_ANY=1
  set /A nFnd+=1

  set ID=%%A
  set BND_BOX=%%B
  set CENTROID=%%C
  set AREA=%%D
  set COL=%%E

  if /I "!COL!"=="gray(0)" (
    set CH_EXCL=!CH_EXCL!,!ID!
    set /A nExcl+=1
  )
)

if %HAS_ANY%==0 (
  echo ** No components?? **
  exit /B 1
)

:: remove leading comma
set CH_EXCL=%CH_EXCL:~1%
echo %0: CH_EXCL=%CH_EXCL%

:: FIXME? not necessarily an error.
if "%CH_EXCL%"=="" exit /B 1

%IM%convert ^
  %INFILE% ^
  -define "connected-components:remove=%CH_EXCL%" ^
  -connected-components 4 ^
  -background Black -alpha Background ^
  -auto-level ^
  +write %IMG_COMPST% ^
  -compose DstOver ^
  ( -clone 0 ) -geometry +1+0 -composite ^
  ( -clone 0 ) -geometry +0+1 -composite ^
  ( -clone 0 ) -geometry +2+0 -composite ^
  ( -clone 0 ) -geometry +0+2 -composite ^
  -compose Over ^
  +write %IMG_COMPSF% ^
  -unique-colors ^
  +write %IMG_GRAYS% ^
  %OUTFILE%



call echoRestore

endlocal & set s2pOUTFILE=%OUTFILE%

parts2FlatCols.bat

rem Given %1 is an image
rem %2 is name of output from skel2Parts from skeleton of %1
rem makes %3 output name.

@if "%2"=="" findstr /B "rem @rem" %~f0 & exit /B 1

@setlocal enabledelayedexpansion

@call echoOffSave

call %PICTBAT%setInOut %1 p2fc

set FILE_FMT=%2

if not "%3"=="" if not "%3"=="." set OUTFILE=%3

for /F %%F in ("%FILE_FMT%") do (
  set IMG_COMPST=%%~dpnF_compst%%~xF
  set IMG_COMPSF=%%~dpnF_compsf%%~xF
  set IMG_GRAYS=%%~dpnF_grays%%~xF
  set IMG_2XN=%%~dpnF_2xn%%~xF

  set TMP_SCRA=%%~nF_p2fca.scr
  set TMP_SCRB=%%~nF_p2fcb.scr
)

del %IMG_2XN% >nul

echo %IMG_COMPSF% ^
  ( %INFILE% -colorspace RGB ) ^
  +write mpr:IMGS -delete 0-1 ^
  -fill White -background Black >%TMP_SCRA%

for /F "usebackq skip=1 tokens=3" %%C in (`%IM%convert ^
  %IMG_GRAYS% ^
  txt:`) do (
  echo %%C

  echo ^( ^
mpr:IMGS[0] ^
+transparent %%C ^
-colorize 100 ^
-compose Over -layers flatten ^
mpr:IMGS[1] ^
+swap ^
-alpha off ^
-compose CopyOpacity -composite -scale "1x1^!" +depth ^) >>%TMP_SCRA%
)

echo -alpha off -append ( %IMG_GRAYS% -rotate 90 ) +swap -colorspace sRGB +append +depth >>%TMP_SCRA%

type %TMP_SCRA%

:: Next is okay for v6. But v7 needs "-script".
%IM%convert @%TMP_SCRA% %IMG_2XN%

if not exist %IMG_2XN% (
  %0: IMG_2XN [%IMG_2XN%] not created
  exit /B 1
)

%IM%identify %IMG_2XN%


echo %IMG_COMPSF% >%TMP_SCRB%

set HasTrans=0
for /F "usebackq skip=1 tokens=3" %%C in (`%IM%convert ^
  %IMG_2XN% ^
  txt:`) do (
  rem echo %%C
  if !HasTrans!==0 (
    set HasTrans=1
    set GRAY=%%C
  ) else (
    set HasTrans=0
    echo !GRAY! to %%C
    echo -fill %%C -opaque !GRAY! >>%TMP_SCRB%
  )
)

if %HasTrans%==1 (
  echo HasTrans=%HasTrans%
  exit /B 1
)

type %TMP_SCRB%

echo %0: TMP_SCRB=%TMP_SCRB%

:: Next is okay for v6. But v7 needs "-script".
%IM%convert @%TMP_SCRB% %OUTFILE%





call echoRestore

endlocal & set p2fcOUTFILE=%OUTFILE%

All images on this page were created by the commands shown, using:

%IM%identify -version
Version: ImageMagick 6.9.5-3 Q16 x86 2016-07-22 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib cairo flif freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib

To improve internet download speeds, some images may have been automatically converted (by ImageMagick, of course) from PNG to JPG.

Source file for this web page is flatcolcart.h1. To re-create this web page, run "procH1 flatcolcart".


This page, including the images, is my copyright. Anyone is permitted to use or adapt any of the code, scripts or images for any purpose, including commercial use.

Anyone is permitted to re-publish this page, but only for non-commercial use.

Anyone is permitted to link to this page, including for commercial use.


Page version v1.0 21-July-2016.

Page created 25-Oct-2016 20:25:24.

Copyright © 2016 Alan Gibson.