Q:

Using Imagemagick to create derivatives of an image that contain only occurrences of every unique color

So here's the gist of it. I've been pulling my hair out trying to design a script using Imagemagick to read an image, and create new images based off of the original. There would be an image for each unique color, and each image would be named after the color it represents (in hex). A simple example would be one like this:
The script would be given an image. Since I have less than 10 reputation at the time of writing, I'll just try a different way of visualizing it.
R=FF0000
G=00FF00
B=0000FF
P=FC00FF

And here's the "image":
RRRRRRRR
GGGGGGGG
BBBBBBBB
RRRRRRRR
GGGGGGGG
BBBBBBBB
RRRRRRRR
GGGGGGGG

Each letter in the array represents a single pixel in the image.
Since there are three unique colors in this image, the script would create 3 separate images, each named after the color they are to represent.
Here are the three images that would be created:
RRRRRRRR
PPPPPPPP
PPPPPPPP
RRRRRRRR
PPPPPPPP
PPPPPPPP
RRRRRRRR
PPPPPPPP

(named FF0000.png)
PPPPPPPP
GGGGGGGG
PPPPPPPP
PPPPPPPP
GGGGGGGG
PPPPPPPP
PPPPPPPP
GGGGGGGG

(named 00FF00.png)
and
PPPPPPPP
PPPPPPPP
BBBBBBBB
PPPPPPPP
PPPPPPPP
BBBBBBBB
PPPPPPPP
PPPPPPPP

(named 0000FF.png)
The background color is set to #FC00FF to prevent confusion when it encounters white. Anyways, How can I go about doing this, and if anyone's generous enough, can example code be given? I apologize if this is the wrong place to be posting stuff like this. If it is, tell me, and I'll take the question down and ask it somewhere else. Also, if there's any vagueness that needs clearing up, tell me and I'll do my best to explain further. Thanks!

A:

Fred's answer is exactly what you want for the crux of your question, so please up-vote and accept that.
My answer may provide some "ancillary framework" around his to get the rest of the job done.
Let's generate an image with 8 colours by making a red-blue gradient with just 7 jumps:
convert -size 8x1 gradient:blue-red -scale 400x50\! result.png

As it is 400x50, that image has 20,000 pixels but only 8 unique colours. I wanted to show you how to get that list of 8 colours to wrap that around Fred's answer. So, here is how to do that:
convert result.png -depth 8 -unique-colors txt:

Output
# ImageMagick pixel enumeration: 8,1,65535,srgb
0,0: (65535,0,0)  #FF0000  red
1,0: (56283,0,9252)  #DB0024  srgb(219,0,36)
2,0: (46774,0,18761)  #B60049  srgb(182,0,73)
3,0: (37522,0,28013)  #92006D  srgb(146,0,109)
4,0: (28013,0,37522)  #6D0092  srgb(109,0,146)
5,0: (18761,0,46774)  #4900B6  srgb(73,0,182)
6,0: (9252,0,56283)  #2400DB  srgb(36,0,219)
7,0: (0,0,65535)  #0000FF  blue

Now you can extract the hex from the above like this:
convert result.png -depth 8 -unique-colors txt: | grep -Po "(?<=#)[0-9A-F]{6}"

FF0000
DB0024
B60049
92006D
6D0092
4900B6
2400DB
0000FF

So, here is what I am trying to get to - put the above around Fred's script to get something like this:
convert YourImage.png -depth 8 -unique-colors txt: | 
   grep -Po "(?<=#)[0-9A-F]{6}" | 
      while read hex; do
         convert YourImage.png -depth 8 -fill "#FC00FF" +opaque "#$hex" ${hex}.png
done

