Do you need to know how many different colours are used in an image?

Here's how do it for nearly any image type using the command line:

pngtopnm foo.png | pnmtoplainpnm | tail +4 | tr -s " " | tr " " "\n" | sort -n | uniq | wc -l

If your image is something other than png, then replace the first command with jpegtopnm, giftopnm, bmptopnm, or whatever.

The main pages for tail, tr, sort, uniq, and wc will tell you what each of the commands above are doing. Unfortunately, the pngtopnm and pnmtoplainpnm man pages (along with the rest of the netpbm tools) are only pointers to other documentation. Also, you'll need to have the netpbm package installed on your distro. It's pretty common, but might not be available by default on a more stripped down distro.

Cheers,
Carl.

Reply via email to