Identify unique colors only

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
luboslives
Posts: 4
Joined: 2019-08-07T07:56:02-07:00
Authentication code: 1152

Identify unique colors only

Post by luboslives » 2019-08-07T08:21:21-07:00

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)

snibgo
Posts: 11994
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Identify unique colors only

Post by snibgo » 2019-08-07T09:20:44-07:00

See http://www.imagemagick.org/script/escape.php

Code: Select all

magick rose: -format %k info:
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25376
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Identify unique colors only

Post by fmw42 » 2019-08-07T09:35:08-07:00

Also

Code: Select all

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

luboslives
Posts: 4
Joined: 2019-08-07T07:56:02-07:00
Authentication code: 1152

Re: Identify unique colors only

Post by luboslives » 2019-08-07T09:53:16-07:00

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

User avatar
fmw42
Posts: 25376
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Identify unique colors only

Post by fmw42 » 2019-08-07T09:59:15-07:00

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.

snibgo
Posts: 11994
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Identify unique colors only

Post by snibgo » 2019-08-07T10:10:02-07:00

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
snibgo's IM pages: im.snibgo.com

luboslives
Posts: 4
Joined: 2019-08-07T07:56:02-07:00
Authentication code: 1152

Re: Identify unique colors only

Post by luboslives » 2019-08-07T10:12:17-07:00

@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.

luboslives
Posts: 4
Joined: 2019-08-07T07:56:02-07:00
Authentication code: 1152

Re: Identify unique colors only

Post by luboslives » 2019-08-07T10:13:51-07:00

Thanks @snibgo! Is that doable through imagick or only command line?

snibgo
Posts: 11994
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Identify unique colors only

Post by snibgo » 2019-08-07T10:19:36-07:00

Sorry, I don't know PHP or IMagick.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25376
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Identify unique colors only

Post by fmw42 » 2019-08-07T10:43:02-07:00

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

Post Reply