identify -verbose gives colors in #xxxxxxxx form

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
firewood

identify -verbose gives colors in #xxxxxxxx form

Post by firewood »

Until recently, I have been extracting the RGB colors from gif files using indentify -verbose. The results always gave me an array of colors in the expected form, #xxxxxx, which I then used in CSS, for setting color styles.

In PHP, the function I have been using to extract the colors is as follows:
preg_match('/([0-9]+):.*(#[0-9ABCDEFabcdef]+)[ ]*/', $hist, $color);
Previously, the $color variable always came through as a 6 digit RGB color.

Now I see that the colors are coming through with 8 digits instead of 6, #xxxxxxxx. Will someone please fill me in on what is happening and how to translate this new color scheme into a scheme that works with CSS?

Additionally, is there a more appropriate way to extract RGB colors from an image file rather than identify -verbose?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: identify -verbose gives colors in #xxxxxxxx form

Post by magick »

Add +matte to your identify command line to turn off the alpha channel. An alpha channel expands to 8 hexadecimal values, 2 for each channel (RGBA).

You can extract the color values from the TXT image format:
  • convert logo: logo.txt
    more logo.txt
You can also retrieve the color values with PerlMagick or the C / C++ API's.
firewood

Re: identify -verbose gives colors in #xxxxxxxx form

Post by firewood »

Thanks, knowing that the last two digits were for the alpha channel helped me solve the problem, but not by using +matte. (I used substr($color,0,7) to get the RGB section of the color.)

When I tried to use +matte, I got no results at all. I guess the command string is incorrectly set up. Here is the command I used:
$exec_str = 'identify -verbose +matte ' . $image_dir . $image_file . ' > ' . $image_dir . 'hist.txt';

When I execute this I get no results. For example, after
exec($exec_str);
$hist_txt = file_get_contents($image_dir . 'hist.txt');
the $hist_txt variable is empty.

The following command, however, did work:
$exec_str = 'identify -verbose ' . $image_dir . $image_file . ' > ' . $image_dir . 'hist.txt';
Post Reply