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 %IM7DEV% has been built with various process modules. See Process modules.

Sample input

set SRC=barbara_test.png
barbara_test.pngjpg

This is a common test image, and is not my copyright.

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.

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

How many colours do we have?

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

How many colours do we have?

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

How many colours do we have?

%IMG7%magick ^
  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:

%IMG7%magick ^
  %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.

%IMG7%magick ^
  %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.

%IMG7%magick ^
  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.

%IMG7%magick ^
  fcc_partbnd4.png ^
  -define "connected-components:verbose=true" ^
  -connected-components 4 ^
  -auto-level ^
  fcc_gray.png 
Objects (id: bounding-box centroid area mean-color):
  95: 190x447+265+125 364.1,267.0 26039 gray(0)
  1: 779x576+0+0 406.6,264.1 25536 gray(255)
  196: 170x278+10+264 68.0,389.5 23857 gray(0)
  257: 132x164+645+412 719.2,505.2 16255 gray(0)
  228: 146x186+382+340 453.4,428.3 13781 gray(0)
  195: 116x203+195+263 253.3,355.9 13006 gray(0)
  226: 142x227+530+338 601.9,460.5 12339 gray(0)
  250: 103x133+199+401 250.8,474.5 9562 gray(0)
  227: 171x236+350+340 416.1,492.0 9513 gray(0)
  13: 77x136+295+0 335.2,66.9 9272 gray(0)
  14: 82x131+371+0 405.2,64.3 8699 gray(0)
  183: 61x331+271+245 313.2,425.4 8530 gray(0)
  80: 146x85+8+105 78.2,143.0 8317 gray(0)
  163: 240x123+88+211 216.5,278.9 8192 gray(0)
  262: 116x99+48+433 120.3,485.9 6901 gray(0)
  201: 75x170+702+270 751.5,336.2 6861 gray(0)
  241: 44x194+159+382 180.0,476.0 6494 gray(0)
  214: 95x115+660+311 710.5,372.6 6233 gray(0)
  278: 145x60+21+516 97.1,546.0 6008 gray(0)
  64: 106x92+165+61 210.6,98.4 5393 gray(0)
  25: 13x576+774+0 782.0,290.0 5190 gray(0)
  42: 61x256+681+24 716.8,161.1 5131 gray(0)
  9: 115x66+169+0 227.4,30.4 5058 gray(0)
  248: 104x180+590+396 645.5,495.4 5024 gray(0)
  221: 112x160+535+325 580.8,395.9 4741 gray(0)
  6: 69x111+87+0 122.3,55.0 4518 gray(0)
  149: 63x81+714+194 747.4,233.8 4362 gray(0)
  191: 91x90+378+254 418.3,298.9 4062 gray(0)
  65: 85x185+557+61 602.2,143.0 3785 gray(0)
  193: 143x55+467+259 542.7,285.9 3713 gray(0)
  276: 77x74+331+502 371.0,547.8 3625 gray(0)
  4: 53x102+59+0 84.0,39.9 3366 gray(0)
  178: 75x117+605+236 644.5,297.3 3251 gray(0)
  123: 90x108+450+165 491.2,226.7 3205 gray(0)
  24: 30x117+748+0 763.4,58.7 3031 gray(0)
  59: 96x57+501+55 549.5,79.9 2981 gray(0)
  40: 79x95+14+24 42.9,75.5 2927 gray(0)
  118: 136x56+173+158 237.6,186.2 2882 gray(0)
  54: 59x72+617+46 649.9,80.8 2820 gray(0)
  205: 14x294+0+282 4.3,431.8 2786 gray(0)
  56: 45x99+666+47 689.3,91.2 2755 gray(0)
  50: 139x45+511+35 574.9,52.3 2718 gray(0)
  194: 135x58+58+261 129.7,293.6 2669 gray(0)
  21: 96x37+591+0 632.7,17.8 2666 gray(0)
  44: 43x98+462+29 481.6,76.7 2518 gray(0)
  159: 57x72+123+208 146.7,235.7 2476 gray(0)
  169: 65x67+410+217 442.1,251.0 2471 gray(0)
  70: 55x95+248+79 276.9,124.1 2363 gray(0)
  88: 75x65+640+117 672.8,147.1 2328 gray(0)
  224: 92x108+109+330 157.3,383.1 2296 gray(0)
  237: 41x98+518+367 541.0,415.5 2280 gray(0)
  117: 60x59+54+157 85.9,182.4 2273 gray(0)
  174: 74x44+57+229 94.7,250.4 2236 gray(0)
  211: 52x140+509+309 529.3,373.4 2205 gray(0)
  19: 78x38+513+0 552.7,20.8 2139 gray(0)
  256: 99x115+370+409 417.0,465.2 2093 gray(0)
  212: 81x52+557+309 591.1,330.6 2024 gray(0)
  45: 29x99+441+30 453.5,78.6 2013 gray(0)
  286: 81x34+202+542 245.2,560.1 2010 gray(0)
  162: 67x60+188+211 219.3,236.6 1960 gray(0)
  0: 29x114+0+0 10.8,58.5 1915 gray(0)
  135: 77x53+503+176 543.6,204.6 1896 gray(0)
  225: 54x90+451+331 481.9,368.9 1851 gray(0)
  68: 44x120+126+63 154.2,119.3 1833 gray(0)
  73: 39x82+561+89 583.1,126.8 1789 gray(0)
  89: 86x56+165+120 208.8,144.2 1760 gray(0)
  172: 56x66+276+225 304.9,258.2 1725 gray(0)
  106: 43x77+493+136 509.0,172.0 1625 gray(0)
  181: 47x55+656+239 678.2,268.1 1616 gray(0)
  206: 52x73+466+288 496.3,318.9 1593 gray(0)
  281: 78x35+194+522 226.4,536.3 1588 gray(0)
  210: 33x114+501+305 514.5,360.4 1578 gray(0)
  69: 12x193+0+78 3.7,182.8 1559 gray(0)
  111: 44x53+733+140 756.3,165.8 1528 gray(0)
  153: 35x74+631+202 646.8,238.7 1516 gray(0)
  161: 51x53+158+210 182.4,237.6 1492 gray(0)
  207: 70x56+667+288 700.7,313.4 1472 gray(0)
  72: 46x54+504+86 527.0,113.1 1404 gray(0)
  255: 43x77+626+407 645.7,446.8 1399 gray(0)
  204: 72x42+559+281 595.5,303.2 1375 gray(0)
  28: 28x97+45+6 58.6,52.2 1369 gray(0)
  43: 19x114+734+25 742.4,82.8 1316 gray(0)
  92: 24x92+477+121 487.4,164.0 1223 gray(0)
  124: 34x67+585+165 603.6,197.9 1189 gray(0)
  154: 35x45+10+203 24.5,227.0 1160 gray(0)
  271: 34x66+499+483 516.3,517.4 1153 gray(0)
  23: 67x37+686+0 711.3,13.2 1151 gray(0)
  94: 34x52+447+123 462.1,147.7 1149 gray(0)
  127: 27x79+608+166 621.4,210.2 1107 gray(0)
  7: 28x64+148+0 160.6,31.6 1088 gray(0)
  47: 112x14+594+33 645.4,40.3 1084 gray(0)
  98: 32x55+628+126 642.3,157.1 1076 gray(0)
  213: 42x34+421+310 442.1,325.7 1036 gray(0)
  107: 61x43+198+137 231.5,160.3 1032 gray(0)
  41: 20x85+704+24 712.0,63.8 1023 gray(0)
  110: 44x52+514+140 534.9,163.4 1016 gray(0)
  186: 39x36+10+247 28.0,260.7 918 gray(0)
  141: 62x23+111+185 139.8,199.3 864 gray(0)
  134: 38x42+633+175 654.6,195.1 857 gray(0)
  166: 45x26+46+215 65.7,226.5 855 gray(0)
  293: 98x18+28+558 66.7,569.5 849 gray(0)
  209: 34x43+461+303 478.1,324.5 844 gray(0)
  140: 39x32+32+185 52.9,202.2 842 gray(0)
  58: 38x33+263+54 281.0,68.1 833 gray(0)
  128: 48x31+667+166 688.5,180.1 797 gray(0)
  157: 40x26+85+205 106.5,217.8 779 gray(0)
  288: 34x33+626+543 642.1,561.0 737 gray(0)
  292: 44x21+520+555 540.3,566.2 730 gray(0)
  167: 41x35+451+216 469.1,233.4 725 gray(0)
  234: 33x42+178+360 195.6,381.4 720 gray(0)
  222: 27x44+541+328 552.2,350.9 710 gray(0)
  272: 31x60+521+483 535.0,508.4 705 gray(0)
  179: 38x31+555+237 574.1,251.5 669 gray(0)
  145: 32x31+671+190 687.5,204.1 655 gray(0)
  165: 42x28+518+213 540.9,228.6 653 gray(0)
  46: 35x26+268+31 287.1,44.2 639 gray(0)
  290: 45x23+403+553 423.4,566.4 630 gray(0)
  160: 28x34+656+209 669.5,226.2 629 gray(0)
  263: 23x41+350+441 359.8,460.4 623 gray(0)
  17: 45x23+463+0 490.1,7.6 594 gray(0)
  249: 29x35+131+400 148.6,419.1 594 gray(0)
  85: 19x37+759+114 768.5,131.3 566 gray(0)
  15: 40x28+416+0 440.0,9.7 564 gray(0)
  22: 34x25+662+0 679.8,9.7 560 gray(0)
  113: 41x37+182+145 201.3,163.2 553 gray(0)
  78: 24x40+542+100 553.6,120.1 548 gray(0)
  93: 23x41+278+123 289.7,145.4 545 gray(0)
  223: 27x34+381+329 392.1,346.8 544 gray(0)
  287: 28x33+10+543 20.1,562.3 527 gray(0)
  30: 31x25+270+7 287.3,19.2 486 gray(0)
  77: 23x38+245+100 257.8,118.2 486 gray(0)
  148: 30x22+200+193 215.3,203.7 481 gray(0)
  87: 26x33+496+116 506.6,133.5 470 gray(0)
  137: 50x15+123+179 145.1,188.1 470 gray(0)
  185: 36x22+520+246 539.6,259.6 465 gray(0)
  289: 39x26+539+545 558.7,555.2 451 gray(0)
  279: 21x29+482+518 492.2,533.2 446 gray(0)
  103: 28x40+298+133 309.7,155.9 439 gray(0)
  136: 25x19+732+176 744.1,184.8 435 gray(0)
  283: 24x28+401+531 411.0,545.2 414 gray(0)
  199: 46x15+506+268 526.4,274.0 405 gray(0)
  26: 20x30+456+5 464.4,18.5 390 gray(0)
  277: 24x31+458+511 471.2,527.1 384 gray(0)
  202: 28x20+23+273 38.0,283.2 383 gray(0)
  18: 26x20+508+0 520.4,7.5 374 gray(0)
  29: 37x23+715+6 734.3,17.1 371 gray(0)
  158: 24x21+248+206 259.5,215.6 351 gray(0)
  8: 65x7+173+0 202.2,2.4 351 gray(0)
  35: 34x19+469+14 485.9,24.1 343 gray(0)
  67: 17x31+138+63 146.1,78.3 334 gray(0)
  295: 50x16+559+560 581.8,570.9 328 gray(0)
  20: 24x18+572+0 584.5,6.8 318 gray(0)
  90: 16x42+560+120 567.2,140.5 317 gray(0)
  247: 17x33+597+396 605.3,412.4 312 gray(0)
  142: 31x16+10+188 24.6,194.4 303 gray(0)
  61: 31x16+180+56 195.2,64.7 301 gray(0)
  53: 14x34+490+44 496.5,62.9 297 gray(0)
  294: 33x16+175+560 193.3,569.1 292 gray(0)
  198: 20x22+46+268 55.9,277.3 290 gray(0)
  114: 20x19+609+147 619.2,155.5 285 gray(0)
  130: 24x22+38+169 49.8,181.6 280 gray(0)
  232: 13x26+290+351 296.5,362.9 276 gray(0)
  264: 27x32+412+448 424.8,463.2 268 gray(0)
  156: 23x22+432+204 443.1,214.9 263 gray(0)
  184: 25x26+597+245 610.0,257.9 260 gray(0)
  146: 22x15+243+191 254.2,197.8 255 gray(0)
  99: 19x22+744+126 753.5,136.0 248 gray(0)
  236: 21x27+137+364 147.0,376.9 245 gray(0)
  190: 18x17+45+252 53.5,259.6 242 gray(0)
  52: 17x20+187+41 194.6,50.9 240 gray(0)
  36: 19x22+500+14 509.1,24.1 223 gray(0)
  285: 20x16+518+541 527.3,549.8 217 gray(0)
  122: 15x23+152+163 158.2,175.3 216 gray(0)
  119: 16x24+537+158 544.3,169.8 209 gray(0)
  108: 13x27+554+137 560.5,149.9 206 gray(0)
  126: 16x22+547+166 554.1,176.5 206 gray(0)
  231: 21x20+612+350 621.8,359.6 205 gray(0)
  173: 17x23+586+226 593.2,237.8 197 gray(0)
  254: 16x15+645+406 652.6,413.3 174 gray(0)
  129: 24x16+559+168 569.7,175.1 171 gray(0)
  235: 25x9+443+362 454.6,366.0 167 gray(0)
  170: 16x21+475+221 482.8,231.4 166 gray(0)
  152: 14x20+450+199 456.5,209.8 166 gray(0)
  75: 23x11+192+97 203.5,101.4 162 gray(0)
  27: 16x16+9+6 17.3,12.6 160 gray(0)
  200: 19x12+0+269 8.6,275.3 160 gray(0)
  49: 15x15+499+35 506.5,42.0 157 gray(0)
  189: 17x15+592+249 598.6,254.6 153 gray(0)
  33: 10x23+36+9 41.1,21.0 151 gray(0)
  97: 11x24+625+125 629.8,135.7 151 gray(0)
  176: 15x16+547+235 554.1,242.2 147 gray(0)
  242: 17x18+664+382 672.4,390.6 142 gray(0)
  143: 16x13+628+188 635.6,193.9 141 gray(0)
  71: 13x16+457+86 463.2,93.0 141 gray(0)
  84: 15x16+488+109 494.3,115.9 139 gray(0)
  3: 21x9+39+0 48.3,3.0 139 gray(0)
  138: 16x14+531+179 538.5,185.9 137 gray(0)
  57: 20x12+501+49 510.5,53.7 136 gray(0)
  74: 12x16+23+95 28.9,102.7 134 gray(0)
  5: 26x8+109+0 119.8,2.3 133 gray(0)
  139: 18x9+241+182 249.6,186.4 133 gray(0)
  91: 15x14+703+120 710.4,126.2 132 gray(0)
  240: 15x16+287+377 294.3,384.5 132 gray(0)
  66: 15x12+170+62 176.8,68.4 131 gray(0)
  51: 13x19+177+38 182.5,47.2 129 gray(0)
  150: 15x14+439+195 446.7,200.6 128 gray(0)
  177: 15x14+601+236 607.9,243.4 126 gray(0)
  171: 17x18+590+221 597.9,229.0 125 gray(0)
  175: 16x12+36+235 44.0,241.7 121 gray(0)
  238: 12x11+447+370 452.4,375.3 121 gray(0)
  282: 14x12+273+531 278.2,536.1 119 gray(0)
  102: 11x18+702+131 706.8,140.3 118 gray(0)
  81: 15x19+205+107 212.3,114.8 117 gray(0)
  144: 11x18+574+190 578.5,198.2 117 gray(0)
  203: 18x11+4+280 12.6,284.1 116 gray(0)
  10: 20x9+259+0 269.9,3.0 115 gray(0)
  296: 18x11+313+565 322.0,570.5 114 gray(0)
  151: 12x17+22+198 27.7,206.5 114 gray(0)
  2: 13x9+25+0 30.9,3.7 108 gray(0)
  267: 16x14+525+456 532.2,462.2 105 gray(0)
  269: 10x16+528+466 531.4,473.8 104 gray(0)
  246: 11x13+283+396 287.2,402.6 104 gray(0)
  79: 12x20+213+102 218.6,111.6 103 gray(0)
  133: 13x13+168+175 173.3,180.6 102 gray(0)
  11: 14x11+278+0 284.9,4.2 101 gray(0)
  37: 14x12+21+16 27.3,21.4 100 gray(0)
  86: 12x11+747+115 752.7,120.5 97 gray(0)
  55: 13x12+166+47 172.7,52.9 96 gray(0)
  112: 12x12+307+143 311.8,148.3 93 gray(0)
  260: 14x13+125+426 131.4,432.3 91 gray(0)
  284: 12x9+230+541 235.5,545.0 91 gray(0)
  32: 12x10+26+9 32.3,12.7 87 gray(0)
  219: 13x11+35+322 40.7,327.1 83 gray(0)
  182: 14x10+49+242 55.3,247.2 82 gray(0)
  16: 16x8+447+0 455.3,2.5 82 gray(0)
  48: 17x6+687+34 694.6,36.8 81 gray(0)
  261: 11x12+592+426 596.4,431.6 81 gray(0)
  115: 10x10+705+149 709.7,153.4 77 gray(0)
  217: 13x9+629+316 635.4,320.8 76 gray(0)
  63: 14x8+164+57 169.8,60.4 73 gray(0)
  100: 12x9+311+131 316.6,135.1 72 gray(0)
  168: 10x11+560+216 564.8,221.6 70 gray(0)
  233: 12x10+626+359 631.3,363.9 64 gray(0)
  187: 10x11+470+248 475.0,252.6 63 gray(0)
  132: 8x12+628+173 630.8,178.9 62 gray(0)
  268: 12x13+429+457 434.3,463.2 61 gray(0)
  147: 9x10+514+191 518.0,195.1 60 gray(0)
  239: 7x9+460+372 462.8,375.7 58 gray(0)
  273: 17x11+77+495 85.0,499.9 57 gray(255)
  109: 10x10+316+138 320.9,142.5 55 gray(0)
  131: 10x10+161+169 165.2,173.8 55 gray(0)
  197: 10x8+553+264 557.6,268.1 53 gray(0)
  220: 10x8+46+322 50.4,325.8 52 gray(0)
  266: 9x10+466+453 470.1,457.4 52 gray(0)
  104: 8x9+305+135 308.2,138.7 51 gray(0)
  12: 11x6+291+0 295.2,2.1 49 gray(0)
  243: 9x8+286+391 290.3,394.3 47 gray(0)
  62: 10x8+504+56 508.4,60.0 46 gray(0)
  245: 8x10+665+395 668.2,399.7 46 gray(0)
  180: 10x7+536+239 540.6,242.2 45 gray(0)
  39: 7x10+31+23 34.4,27.8 45 gray(0)
  259: 9x8+707+419 710.5,423.0 45 gray(0)
  116: 8x8+250+156 253.8,159.3 44 gray(0)
  215: 9x9+26+314 30.3,317.9 43 gray(0)
  38: 10x9+741+17 745.7,21.0 42 gray(0)
  252: 9x15+736+403 740.0,408.6 41 gray(255)
  230: 8x8+626+347 630.1,350.3 40 gray(0)
  155: 7x7+94+204 96.8,207.1 40 gray(0)
  275: 8x7+78+498 81.8,501.0 40 gray(0)
  299: 14x4+622+572 628.5,573.9 40 gray(0)
  76: 9x10+538+98 542.2,102.3 39 gray(0)
  96: 9x7+467+125 471.1,128.0 38 gray(0)
  208: 8x7+645+290 648.0,292.6 38 gray(0)
  82: 8x8+545+107 548.9,110.1 36 gray(0)
  300: 13x4+653+572 659.2,573.9 36 gray(0)
  251: 7x7+292+403 295.3,406.5 35 gray(0)
  229: 7x7+485+345 488.1,347.7 34 gray(0)
  253: 7x7+737+404 739.8,406.8 34 gray(0)
  120: 7x8+244+159 247.2,162.4 34 gray(0)
  105: 6x9+711+135 713.5,138.9 34 gray(0)
  83: 8x6+21+108 24.5,111.0 34 gray(0)
  280: 7x7+550+519 552.7,522.6 33 gray(0)
  274: 7x6+86+496 88.5,498.5 31 gray(0)
  216: 8x8+55+315 58.6,318.6 29 gray(0)
  218: 8x8+52+319 55.6,322.6 29 gray(0)
  60: 5x6+158+56 160.1,58.6 28 gray(0)
  258: 7x5+505+417 508.1,419.1 27 gray(0)
  34: 6x7+475+11 477.3,14.2 27 gray(0)
  101: 7x7+475+131 478.1,133.9 27 gray(0)
  291: 7x5+195+554 198.1,556.1 27 gray(0)
  302: 10x3+238+573 242.5,574.2 24 gray(0)
  121: 7x5+609+162 611.9,164.0 24 gray(0)
  303: 9x3+324+573 328.0,574.2 21 gray(0)
  31: 6x5+278+8 280.6,10.2 20 gray(0)
  244: 6x6+678+392 680.6,395.0 20 gray(0)
  265: 5x5+483+451 484.8,453.3 19 gray(0)
  298: 12x4+237+572 242.5,573.0 18 gray(255)
  188: 5x5+622+248 623.6,250.2 17 gray(0)
  125: 5x5+617+165 618.8,166.8 17 gray(0)
  164: 5x5+429+212 431.2,214.4 17 gray(0)
  192: 5x5+555+259 557.0,261.1 16 gray(0)
  297: 8x4+192+572 195.5,573.3 14 gray(255)
  270: 5x5+513+481 515.2,483.0 13 gray(0)
  301: 6x3+193+573 195.5,574.3 12 gray(0)
  304: 5x3+646+573 648.0,574.4 9 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:

%IMG7%magick identify fcc_parts_*.png 
fcc_parts_compsf.png PNG 787x576 787x576+0+0 16-bit Grayscale Gray 39031B 0.000u 0:00.000
fcc_parts_compst.png PNG 787x576 787x576+0+0 16-bit Grayscale Gray 39031B 0.000u 0:00.000
fcc_parts_grays.png PNG 300x1 300x1+0+0 16-bit Grayscale Gray 382B 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?

%IMG7%magick ^
  fcc_flat.png ^
  -unique-colors ^
  -format %%w info: 
300
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 rfmsITER=%rfmsITER% 
rfmsITER=1300 
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, but required fewer iterations.

Blending

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

Not much flat-colour.

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

Mostly flat-colour.

%IMG7%magick ^
  %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.

%IMG7%magick ^
  fcc_flat.png ^
  ( ctx_edges.png ^
    -negate -transparent White ^
  ) ^
  -compose Over -composite ^
  fcc_sg1.png
fcc_sg1.png
%IMG7%magick ^
  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
@rem
@rem Updated:
@rem   6-August-2022 for IM v7
@rem

@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% 2>nul
del %IMG_COMPSF% 2>nul
del %IMG_GRAYS% 2>nul

set COMPLIST=s2p_complist.lis

%IMG7%magick ^
  %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

%IMG7%magick ^
  %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.
@rem
@rem Updated:
@rem   6-August-2022 for IM v7.
@rem   27-September-2022 insert "-set colorspace sRGB" twice.
@rem

@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=%%~nF_compst%%~xF
  set IMG_COMPSF=%%~nF_compsf%%~xF
  set IMG_GRAYS=%%~nF_grays%%~xF
  set IMG_2XN=%%~nF_2xn%%~xF

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

del %IMG_2XN% 2>nul

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

for /F "usebackq skip=1 tokens=3" %%C in (`%IMG7%magick ^
  %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 +repage ( %IMG_GRAYS% -rotate 90 ) +swap -colorspace sRGB +append +repage +depth >>%TMP_SCRA%

rem type %TMP_SCRA%

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

echo -write %IMG_2XN% -exit >>%TMP_SCRA%
%IMG7%magick -script %TMP_SCRA%

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

%IMG7%magick identify %IMG_2XN%


echo %IMG_COMPSF% -set colorspace sRGB >%TMP_SCRB%

set HasTrans=0
for /F "usebackq skip=1 tokens=3" %%C in (`%IMG7%magick ^
  %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
)

rem type %TMP_SCRB%

echo %0: TMP_SCRB=%TMP_SCRB%

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

echo -write %OUTFILE% -exit >>%TMP_SCRB%
%IMG7%magick -script %TMP_SCRB%


call echoRestore

endlocal & set p2fcOUTFILE=%OUTFILE%

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

%IMG7%magick -version
Version: ImageMagick 7.1.1-20 Q16-HDRI x86 98bb1d4:20231008 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenCL OpenMP(2.0) 
Delegates (built-in): bzlib cairo freetype gslib heic jng jp2 jpeg jxl lcms lqr lzma openexr pangocairo png ps raqm raw rsvg tiff webp xml zip zlib
Compiler: Visual Studio 2022 (193532217)

To improve internet download speeds, some images may have been automatically converted (by ImageMagick, of course) from PNG or TIFF or MIFF 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 02-Mar-2024 17:15:23.

Copyright © 2024 Alan Gibson.