Cut to 8x8 px | Select best fitting existing 8 color palette

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
snakesnoke
Posts: 2
Joined: 2019-09-19T23:37:51-07:00
Authentication code: 1152

Cut to 8x8 px | Select best fitting existing 8 color palette

Post by snakesnoke »

Hi everyone,

I have the following task and hope this can be accomplished fully automated, but I doubt.

1. The png image must be cut to 8px x 8px tiles.
2. Each tile must be changed to use one/most fitting of 8 existing color palettes (There are only 8 palettes, each with only 8 colors. I could manually create .pal files for each. => This is the most complicated part, which hopefully can somehow be done automatically.)
3. All modified tiles must be merged back to one new entire png image.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cut to 8x8 px | Select best fitting existing 8 color palette

Post by snibgo »

Step 1 is simply "-crop 8x8". Step 3 is simply "-layers flatten".

For step 2, I would prepare palette images. Each would have 8 pixels, for the 8 colours. For each tile, remap to each palette image. Compare the 8 results to the tile, to find which has changed the least. Keep the closest and discard the others.

Step 2 could be done in a different order: remap the entre image, then crop into 8x8 tiles.

I would do this in a shell script. Perhaps it can be done in a single command.
snibgo's IM pages: im.snibgo.com
snakesnoke
Posts: 2
Joined: 2019-09-19T23:37:51-07:00
Authentication code: 1152

Re: Cut to 8x8 px | Select best fitting existing 8 color palette

Post by snakesnoke »

Thank you for the quick reply. Can you give an example for step 2? "Keep the closest and discard the others." would still have to be done manually, but maybe it is possible, since the image size is only 256x224 px.
(I believe, remap the entire picture will not work, because then it is not guaranteed, that each 8x8 px "tile" use only 1 existing palette.)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cut to 8x8 px | Select best fitting existing 8 color palette

Post by snibgo »

What version of IM, on what platform? I assume v7 on Windows BAT.

This creates just three palettes, but is easily extended to any number. It takes an input image (toes.png) and remaps it in turn to each palette, and finds how much this has changed ("distorted") the image. It writes the scores to M1, M2 and M3. Then it finds which score is the smallest.

Code: Select all

%IMG7%magick -size 8x1 gradient:red-blue m1.png
%IMG7%magick -size 8x1 gradient:black-white m2.png
%IMG7%magick -size 8x1 gradient:green-blue m3.png

for /F "usebackq" %%L in (`%IMG7%magick ^
  toes.png +write mpr:INP ^
  ^( +clone -remap m1.png ^) ^
  -metric RMSE -format "M1=%%[distortion]\n" -compare ^
  +write info: ^
  +delete ^
  mpr:INP ^
  ^( +clone -remap m2.png ^) ^
  -metric RMSE -format "M2=%%[distortion]\n" -compare ^
  +write info: ^
  +delete ^
  mpr:INP ^
  ^( +clone -remap m3.png ^) ^
  -metric RMSE -format "M3=%%[distortion]\n" -compare ^
  info:`) do set %%L

echo M1,M2,M3 = %M1%,%M2%,%M3%

for /F "usebackq" %%L in (`%IMG7%magick identify ^
  -format "WCH=%%[fx:%M1%<%M2%?1:2]" ^
  xc:`) do set %%L

for /F "usebackq" %%L in (`%IMG7%magick identify ^
  -format "WCH=%%[fx:!M%WCH%!<%M3%?%WCH%:3]" ^
  xc:`) do set %%L

echo Best palette is number %WCH%
The text output is:

Code: Select all

M1,M2,M3 = 0.329252,0.0801737,0.36366

Best palette is number 2
snibgo's IM pages: im.snibgo.com
Post Reply