Remapping a picture to a set color palette.
Posted: 2018-10-20T23:34:09-07:00
So I am writing this program from a friend who does crystal/beading. The se-up is a follows.
1. These beads come in a number of different colors (each has a number and a name). I have a picture that shows each of these colors. I manually cropped a small sample of each color into a file called XXX.png where XXX is the number.
2. I then ran a script on these XXX.png files:
// reduce to a 1x1 and get the RGB values of it. (Note, here XXX is just a three digit number for the color sample file.)
convert XXX.png -resize 1x1\! -format "%[fx:int(255*r+.5)],%[fx:int(255*g+.5)],%[fx:int(255*b+.5)]" info:-
// create a color tile of size 30x15. (RR, GG, BB are the hex values extracted from the previous line's command, XXX is still the color number
convert -size 30x15 xc:#RRGGBB XXX-proof.png
After this I now have a bunch of XXX-proof.png files - these make up the colors I want to use.
3. I then make a color palette:
convert -append colors/*proof.png palette.png
can be seen here:
4. I now find the picture I want to work on. For example this one:
which is 960x759
5. I reduce the size of that picture, again using imagemagick:
convert filename -resize 96x76! +dither -resize-filename
result:
size is 96x76 here, but that is user defined.
6. I now apply the following command:
convert resize-US.jpg +dither -remap palette.png remap-resize-US.jpg
and I get:
I was now expecting that ALL the pixels int he remapped and resized image would be ONE of the colors in the palette file that I gave it, but it isn't.
EVEN if I add: -color 2 to have it pick the best TWO colors only when I inspect the picture I find pixels that are a completely different color than ANY of the ones in the palette. Here is that result (convert resize-US.jpg -dither None -remap palette.png -colors 2 remap2-resize-US.jpg):
Bar writing code to read a picture, compute L*a*b distances of all pixels to every color in the palette and picking the color that is the 'closest' in the CIELAB 94 norm I don't know what to do.... I do NOT cherish the idea of having to do all that nasty math myself . Any help here would be greatly appreciated.
1. These beads come in a number of different colors (each has a number and a name). I have a picture that shows each of these colors. I manually cropped a small sample of each color into a file called XXX.png where XXX is the number.
2. I then ran a script on these XXX.png files:
// reduce to a 1x1 and get the RGB values of it. (Note, here XXX is just a three digit number for the color sample file.)
convert XXX.png -resize 1x1\! -format "%[fx:int(255*r+.5)],%[fx:int(255*g+.5)],%[fx:int(255*b+.5)]" info:-
// create a color tile of size 30x15. (RR, GG, BB are the hex values extracted from the previous line's command, XXX is still the color number
convert -size 30x15 xc:#RRGGBB XXX-proof.png
After this I now have a bunch of XXX-proof.png files - these make up the colors I want to use.
3. I then make a color palette:
convert -append colors/*proof.png palette.png
can be seen here:
4. I now find the picture I want to work on. For example this one:
which is 960x759
5. I reduce the size of that picture, again using imagemagick:
convert filename -resize 96x76! +dither -resize-filename
result:
size is 96x76 here, but that is user defined.
6. I now apply the following command:
convert resize-US.jpg +dither -remap palette.png remap-resize-US.jpg
and I get:
I was now expecting that ALL the pixels int he remapped and resized image would be ONE of the colors in the palette file that I gave it, but it isn't.
EVEN if I add: -color 2 to have it pick the best TWO colors only when I inspect the picture I find pixels that are a completely different color than ANY of the ones in the palette. Here is that result (convert resize-US.jpg -dither None -remap palette.png -colors 2 remap2-resize-US.jpg):
Bar writing code to read a picture, compute L*a*b distances of all pixels to every color in the palette and picking the color that is the 'closest' in the CIELAB 94 norm I don't know what to do.... I do NOT cherish the idea of having to do all that nasty math myself . Any help here would be greatly appreciated.