Getting standard Hex colors..

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?".
dognose
Posts: 238
Joined: 2005-03-08T22:16:37-07:00

Getting standard Hex colors..

Post by dognose » 2006-11-04T21:28:46-07:00

I've been trying to extract colors from an image, well, various images.

I would like to get standard HTML colors, #000000 - #FFFFFF , but I'm getting all sorts of things, like
GREY72
#094A44C59E9F0000
#81812B2B2929

To get the color in the upper left corner, I'm using the command:

convert input.gif -crop 1x1+0+0 txt:color color.txt

I've tried using -colors and -colormap, but it hasn't helped.

What can I do to get it in the format: #FFFFFF

RetroJ
Posts: 112
Joined: 2006-10-25T08:12:50-07:00

Post by RetroJ » 2006-11-05T19:21:26-07:00

This is incorrect:

Code: Select all

txt:color color.txt
Only the last argument on the command line specifies output.

You will need to take the output from a command line like:

Code: Select all

convert foo.gif txt:
and pipe it through, for example, perl, to do some regex substitution.

User avatar
anthony
Posts: 8869
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony » 2006-11-05T20:48:20-07:00

Warning there was a release of IM where the format of the 'TXT' image was incorrect.
This was quickly recified when discovered.

I described the "ImageMagick pixel enumeration" format quite clearly in IM Examples
http://www.cit.gu.edu.au/~anthony/graph ... files/#txt
Which also has example of what it should look like.

You may get hex values, as the last 'comment' about that pixels color, or you may not.
All that is guranteed is that the comment generated is usable by the same IM that generated it as a -fill or other color argument. That is it could be hex, a rgb() type function or a color name.

For example...

Code: Select all

convert rose: -crop 2x2+0+0 txt:-
# ImageMagick pixel enumeration: 2,2,65535,RGB
0,0: (12336,12079,11565)  rgb(48,47,45)                                 
1,0: (12850,12336,11822)  rgb(50,48,46)                                 
0,1: (12079,11822,11308)  rgb(47,46,44)                                 
1,1: (12336,12079,11565)  rgb(48,47,45)                                 
The values in parenthesis before this 'comment' is the only part of the output that matters to IM when reading a 'TXT' format image. They are the channel color values formated as defined by the single line header of the file.

If this format is not suitable for you, you could try to look for other output image formats,
such as the various NetPBM formats. IM output this in binary form, but you can convert them using NetPBM into 'ascii' format for script processing...

Code: Select all

convert rose: -crop 2x2+0+0 ppm:- | pnmnoraw
P3
2 2
65535
12336 12079 11565 12850 12336 11822 
12079 11822 11308 12336 12079 11565 
You may notice the resemblence to the 'TXT:' format (ImageMagick pixel enumeration).

Note. Suggestions about the 'TXT:' are welcome as it is designed to be scriptable.

WARNING: the 'alpha' values of the Im HEX format is now Alpha and not Matte values to bring it in line with SVG and other graphic usage.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

RetroJ
Posts: 112
Joined: 2006-10-25T08:12:50-07:00

Post by RetroJ » 2006-11-05T21:37:03-07:00

anthony wrote: Suggestions about the 'TXT:' are welcome as it is designed to be scriptable.


Hex output like we are talking about would be more appropriate as an extension to info: than txt:. txt: should stay as it is because it has a specific format designed to be parsable by ImageMagick. info: on the other hand has its -format option, specifically designed to let the user roll their own output. There would have to be a format directive introduced to cause info: to loop over every pixel and produce output. A format specification language akin to that provided by Common Lisp's format would be most welcome, for allowing the user fine control over the textual output of info:.

User avatar
anthony
Posts: 8869
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony » 2006-11-05T23:12:32-07:00

This is a problem.

txt: outputs pixel information
info: -identify and identify outputs general image information.
then we have -fx which loops over every pixel and channel in an image
as defined by -channel (though currently it is not -region limited, and probebly should be),
but has no output method.

I don't think there is a solution that fits the requirements exactly. So lets roll our own...
for example, assuming RGB output...

Code: Select all

  convert rose: -crop 2x2+0+0 txt:- | 
       perl -pe 's/:\s*\(\s*(\d+),\s*(\d+),\s*(\d+)\).*/ sprintf(": #%04x%04x%04x",$1,$2,$3) /e'
Producing

Code: Select all

# ImageMagick pixel enumeration: 2,2,65535,RGB
0,0: #30302f2f2d2d
1,0: #323230302e2e
0,1: #2f2f2e2e2c2c
1,1: #30302f2f2d2d
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

dognose
Posts: 238
Joined: 2005-03-08T22:16:37-07:00

Post by dognose » 2006-11-06T00:16:59-07:00

Ok, well, my main problem was with the multiple formats of the color codes.. (my browser was NOT interpreting some named colors correctly)

I want it in the 6 digit hex code. not the 12 or 16 digit version and not the named colors.

Is there an easy way to convert #30302f2f2d2d to a web safe color?

User avatar
anthony
Posts: 8869
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony » 2006-11-06T00:33:30-07:00

You can alway web-safe the image first!
See http://www.cit.gu.edu.au/~anthony/graph ... /#web_safe
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

RetroJ
Posts: 112
Joined: 2006-10-25T08:12:50-07:00

Post by RetroJ » 2006-11-06T10:06:41-07:00

anthony wrote: This is a problem.

txt: outputs pixel information
info: -identify and identify outputs general image information.
then we have -fx which loops over every pixel and channel in an image
as defined by -channel (though currently it is not -region limited, and probebly should be),
but has no output method.

I think adding formatting options to txt: would be a mistake, because then it would be largely redundant of the purpose of info:. It would be better to either declare txt: as an immutable file format, or to deprecate it in favor of a more powerful info:. Since info: already has -format, it would be consistent design to extend info: with the capability to loop over pixels like -fx.

It's an idea, anyway. I can't commit to writing this myself, now, because I'm working on other parts of the program.

User avatar
anthony
Posts: 8869
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony » 2006-11-06T21:14:14-07:00

However info is not designed to report all the pixels in the image, It just calls statistical functions once per image.

Format is also definatally not designed to output more that one set of info per image.
That should also not change, as it would also effect too many other things.

fx: could do something like this if given an output function, however it gets called once for each value, not once for each pixel.

Basically you need something that sort of falls between the two, if IM is to do the job.
prehaps a special function that you can define the color format to use.

However as the output of this is for a script or other program I thing it is probably better to leave it for the other script or program to do the job. Like I did with the perl line above.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

User avatar
anthony
Posts: 8869
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony » 2006-11-06T21:16:50-07:00

cristy has in response to this discussion updated the txt: output to produce even more info

Latest alpha resulted in...

Code: Select all

convert rose: -crop 2x2+0+0 -depth 8 txt:
producing

Code: Select all

# ImageMagick pixel enumeration: 2,2,255,RGB
0,0: ( 48, 47, 45)  #302F2D  rgb(48,47,45)
1,0: ( 50, 48, 46)  #32302E  rgb(50,48,46)
0,1: ( 47, 46, 44)  #2F2E2C  rgb(47,46,44)
1,1: ( 48, 47, 45)  #302F2D  rgb(48,47,45)
For images with color names...

Code: Select all

convert tree.gif -unique-colors txt:
producing

Code: Select all

# ImageMagick pixel enumeration: 6,1,65535,RGB
0,0: (    0,32896,    0)  #000080800000  green
1,0: (11822,35723,22359)  #2E2E8B8B5757  SeaGreen
2,0: (12850,52685,12850)  #3232CDCD3232  LimeGreen
3,0: (    0,65535,65535)  #0000FFFFFFFF  cyan
4,0: (41120,21074,11565)  #A0A052522D2D  sienna
5,0: (65535,65535,    0)  #FFFFFFFF0000  yellow
And with a -depth 8 setting

Code: Select all

# ImageMagick pixel enumeration: 6,1,255,RGB
0,0: (  0,128,  0)  #008000  green
1,0: ( 46,139, 87)  #2E8B57  SeaGreen
2,0: ( 50,205, 50)  #32CD32  LimeGreen
3,0: (  0,255,255)  #00FFFF  cyan
4,0: (160, 82, 45)  #A0522D  sienna
5,0: (255,255,  0)  #FFFF00  yellow
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

RetroJ
Posts: 112
Joined: 2006-10-25T08:12:50-07:00

Post by RetroJ » 2006-11-07T08:59:45-07:00

anthony wrote: However info is not designed to report all the pixels in the image, It just calls statistical functions once per image.

Format is also definatally not designed to output more that one set of info per image.
That should also not change, as it would also effect too many other things.

fx: could do something like this if given an output function, however it gets called once for each value, not once for each pixel.

Basically you need something that sort of falls between the two, if IM is to do the job.
prehaps a special function that you can define the color format to use.

However as the output of this is for a script or other program I thing it is probably better to leave it for the other script or program to do the job. Like I did with the perl line above.

The gist of what I'm saying is that it is overly complicated to have multiple commands that all output textual information about an image, whether it be headers or data about pixels. I'm not suggesting that this would be simple to implement or that it could be done without breaking backward compatibility. For now, it's just a muse. I think cristy's change to txt: that you mention is good for 99% of cases.

User avatar
anthony
Posts: 8869
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony » 2006-11-07T18:25:58-07:00

I agree.
The only reason the hex color output for the comment field in txt: changed in recent versions was that the function that outputs color had been modified so as to be more in like with SVG specifications. However SVG specifications did not handle 16 bit color values very well, and in the process of correcting this the txt: format silently suffered, until I reported the problems visible in IM Example updates.

Adding the hex color in the position it is now in brings the format pretty well back to its format from before the updates had modified it, but with the extra SVG compatible color field.

Thanks Cristy for his silent watching of this discussion and reacting appropriatally.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

User avatar
RoadRacer
Posts: 8
Joined: 2017-05-04T00:48:33-07:00
Authentication code: 1151

Re: Getting standard Hex colors..

Post by RoadRacer » 2017-05-04T08:20:40-07:00

Hallo together,

I've read a lot about formatting pixel enumeration, but I did'nt got what I'm looking for.
Hopefully somebody may help me.

I'm using Win10 64bit, IM7.0.3 Q16 64bit and powershell.

I would like to read only the luminance of 4 pixels and get the result in a single string separated by a ","

When I try the following code (found at http://www.imagemagick.org/Usage/files/#txt):

Code: Select all

convert.exe Image.png -colorspace Lab -scale 2x2! -format '%[pixel:u.p{0,0}],%[pixel:u.p{1,0}],%[pixel:u.p{0,1}],%[pixel:u.p{1,1}]' info:-
I got:

Code: Select all

'cielab(4.83819%,50.4042%,50.0575%),cielab(63.0672%,54.5108%,68.3696%),cielab(88.9289%,46.6769%,46.0211%),cielab(18.5673%,60.7045%,50.5786%)'
When I try:

Code: Select all

convert.exe Image.png -colorspace Lab -scale 2x2! -format '%[fx:r],' info:- 
I got the single value of luminance of the first pixel:

Code: Select all

'0.0483819,'
But I'm to stupid to combine this with the list of pixel-psoitions "pixel:u.p{x,y}" above.

Sure, I can split the string from the first example using powershell, or I crop each of the four pixel and read the luminance seperatly.

Any idea?
Thanks for your suggestions.
Windows 10 (64 bit)
ImageMagick 7.0.3 Q16 (64 bit, standalone)

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

Re: Getting standard Hex colors..

Post by snibgo » 2017-05-04T08:53:11-07:00

You can combine the two methods. "pixel:" returns a colour, which has multiple components. "fx:" returns a scalar number, which is what you want. ".r" returns the first channel, the "L*" channel of L*a*b*.

I also multiply by 100, assuming you want percentages.

Code: Select all

convert.exe Image.png -colorspace Lab -scale 2x2! -format '%[fx:u.p{0,0}.r*100],%[fx:u.p{1,0}.r*100],%[fx:u.p{0,1}.r*100],%[fx:u.p{1,1}.r*100]' info:-
snibgo's IM pages: im.snibgo.com

User avatar
anthony
Posts: 8869
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Getting standard Hex colors..

Post by anthony » 2017-05-04T21:40:54-07:00

If you do not mind a extra comma at the end, and no newline, you can also do something like this...

Code: Select all

convert rose: -colorspace Lab -scale 2x2! -crop 1x1 -format '%[fx:r*100],'  info:-
36.228,51.7021,54.4274,39.9542,
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

Post Reply