Page 1 of 1

Identify unique colors only

Posted: 2019-08-07T08:21:21-07:00
by luboslives
I have a few PNGs and I'd like to store the number of unique colors in a variable for further processing, so what's the correct way to output that info?

I can get the full report with

Code: Select all

identify myimage.png
or

Code: Select all

identify -verbose myimage.png
but neither of these suggested methods for outputting the number of colors work:

Code: Select all

identify -unique myimage.png
(as documented here)
- I get the default report provided by identify itself.

Code: Select all

identify -colors myimage.png
(as suggested here)

Code: Select all

unrecognized option `-colors'
So is my syntax wrong? How do I retrieve just the number of unique colors?

(Running ImageMagick 7.0.8-53 Q16 x86_64 2019-07-09 on macOS)

Re: Identify unique colors only

Posted: 2019-08-07T09:20:44-07:00
by snibgo
See http://www.imagemagick.org/script/escape.php

Code: Select all

magick rose: -format %k info:

Re: Identify unique colors only

Posted: 2019-08-07T09:35:08-07:00
by fmw42
Also

Code: Select all

magick rose: -format "%[colors]" info:
See https://imagemagick.org/script/escape.php

Re: Identify unique colors only

Posted: 2019-08-07T09:53:16-07:00
by luboslives
Thanks that did the trick.

I'm using it to observe some strange behaviour with WordPress/iMagick... their default PNG settings will inflate the color palette by quite a bit when they're creating the different-sized images for the image set (i went from 16 to 171!). I tried adding the 'png:preserve-colormap' option to their WP_Image_Editor_Imagick class and it still hit 171 colors and it somehow managed to inflate the file size by about 175%! haha

Re: Identify unique colors only

Posted: 2019-08-07T09:59:15-07:00
by fmw42
The number of colors can be increases when you do processing such as resizing. You have not said what processing you do in Imagick. So we have no way to help you with that.

Re: Identify unique colors only

Posted: 2019-08-07T10:10:02-07:00
by snibgo
As Fred says. You can remap to the input image to ensure no new colours are added:

Code: Select all

magick in.png +write mpr:INP -resize 200% -remap mpr:INP out.png

Re: Identify unique colors only

Posted: 2019-08-07T10:12:17-07:00
by luboslives
@fmw42

It's the default WordPress processing that I'm investigating, found inside their imagick class. You can view that file here. It's inside protected function thumbnail_image(), their PNG options are around line 374 at the moment.

Maybe you guys can identify which process inflates the color palettes? I don't know I'm new here :)

If I could figure out how to get WP to not bloat the file sizes of PNGs at smaller dimensions than their originals, maybe they'd consider implementing it.

Re: Identify unique colors only

Posted: 2019-08-07T10:13:51-07:00
by luboslives
Thanks @snibgo! Is that doable through imagick or only command line?

Re: Identify unique colors only

Posted: 2019-08-07T10:19:36-07:00
by snibgo
Sorry, I don't know PHP or IMagick.

Re: Identify unique colors only

Posted: 2019-08-07T10:43:02-07:00
by fmw42
The process of creating thumbnails is a form or resizing. It creates new colors due to the resampling/interpolation done to "average" the color at a location from its neighbors. So you need to remap based upon some common color map, which could be the input images' unique colors. I am not an expert on Imagick, so just get the PHP Imagick documentation and look for something name similar. See https://imagemagick.org/Usage/draw/#primitives and https://www.php.net/manual/en/imagick.remapimage.php