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 »

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: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Identify unique colors only

Post by snibgo »

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: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Identify unique colors only

Post by fmw42 »

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 »

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: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Identify unique colors only

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Identify unique colors only

Post 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
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 »

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

Thanks @snibgo! Is that doable through imagick or only command line?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Identify unique colors only

Post by snibgo »

Sorry, I don't know PHP or IMagick.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Identify unique colors only

Post 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
Post Reply