Extract colors from image
-
- Posts: 53
- Joined: 2012-05-15T04:56:50-07:00
- Authentication code: 13
Extract colors from image
I'd like to split an image into each color and every color that is darker, so that this becomes black and everything else white.
I see I can get the colormap with identify, so what I would like to extract each one of these colors into a separate image, but also include all darker colors, e.g. I want RGB(242,255,0) and darker to be black, and all else white (and then do this for each color on the colormap to get hundreds of separate images.)
I thought that I might be able to define a darker color as any that has a lower RGB value (multiplying or adding the RGB elements together), but now I'm not so sure as yellow would then be darker than blue, but that's not logical.
So I have 2 questions:
1. How can I extract the colors into separate B&W images as explained above?
2. Is there any standard on how to consider whether one color is 'darker' than another?
Thanks!
I see I can get the colormap with identify, so what I would like to extract each one of these colors into a separate image, but also include all darker colors, e.g. I want RGB(242,255,0) and darker to be black, and all else white (and then do this for each color on the colormap to get hundreds of separate images.)
I thought that I might be able to define a darker color as any that has a lower RGB value (multiplying or adding the RGB elements together), but now I'm not so sure as yellow would then be darker than blue, but that's not logical.
So I have 2 questions:
1. How can I extract the colors into separate B&W images as explained above?
2. Is there any standard on how to consider whether one color is 'darker' than another?
Thanks!
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Extract colors from image
For definitions of "darker", see http://www.imagemagick.org/script/comma ... #intensity
Windows syntax:
This:
- reads the built-in image "rose:", but you should use your own file
- makes a copy, turning all the pixels to the chosen colour
- converts both images to greyscale
- subtracts this from the original. Now pixels that were the chosen colour or darker are black.
- changes all non-black pixels to white.
Windows syntax:
Code: Select all
convert ^
rose: ^
( +clone -fill rgb(242,255,0) -colorize 100 ) ^
-grayscale Rec709Luminance ^
-compose MinusSrc -composite ^
-fill White +opaque Black ^
y.png
- reads the built-in image "rose:", but you should use your own file
- makes a copy, turning all the pixels to the chosen colour
- converts both images to greyscale
- subtracts this from the original. Now pixels that were the chosen colour or darker are black.
- changes all non-black pixels to white.
snibgo's IM pages: im.snibgo.com
-
- Posts: 53
- Joined: 2012-05-15T04:56:50-07:00
- Authentication code: 13
Re: Extract colors from image
Thank you very much for your help.
Unfortunately I get this error testing your code.
Am I doing something wrong?
I am on Linux if that makes a difference.
Unfortunately I get this error testing your code.
Code: Select all
# convert rose: (+clone -fill rgb(242,255,0) -colorize 100) -grayscale Rec709Luminance -compose MinusSrc -composite -fill White +opaque Black y.png
-bash: syntax error near unexpected token `('
I am on Linux if that makes a difference.
Re: Extract colors from image
With Linux you need to escape the ( and ) so it becomes /( and /) or is it \( and \) ?
-
- Posts: 53
- Joined: 2012-05-15T04:56:50-07:00
- Authentication code: 13
Re: Extract colors from image
OK, now I'm getting a different error:
Code: Select all
# convert rose: \( +clone -fill "rgb(242,255,0)" -colorize 100 \) -grayscale Rec709Luminance -compose MinusSrc -composite -fill White +opaque Black y.png
convert: unrecognized option `-grayscale' @ error/convert.c/ConvertImageCommand/1632.
Re: Extract colors from image
I do not think "rgb(242,255,0)" this needs the quotes but it needs the \ so rgb\(242,255,0\)
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Extract colors from image
What version IM are you using? If before 6.8.something, I suggest you upgrade.alasdairdf wrote:convert: unrecognized option `-grayscale'
snibgo's IM pages: im.snibgo.com
-
- Posts: 53
- Joined: 2012-05-15T04:56:50-07:00
- Authentication code: 13
Re: Extract colors from image
I can't upgrade, I'm using 6.7.7-7 and I need this exact version as it contains a specific bug fix which then broke again in the next version. This was the version magick did to fix the bug-fix and it is the only version that works.
Bonzo, either works. The problem is with -grayscale.
Bonzo, either works. The problem is with -grayscale.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Extract colors from image
Instead of "-grayscale Rec709Luminance", you could use "-colorspace Gray".
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Extract colors from image
quoting "rgb(242,255,0)" works fine on Unix. I use that rather than escapes. It may be better since it will allow spaces between commas.I do not think "rgb(242,255,0)" this needs the quotes but it needs the \ so rgb\(242,255,0\)
-
- Posts: 53
- Joined: 2012-05-15T04:56:50-07:00
- Authentication code: 13
Re: Extract colors from image
OK, thanks. But is this identical or is "-grayscale Rec709Luminance" going to give better results? It's possible that I could install two different versions of IM and use a newer version for this and the older version for when I need that, but I would only want to do that if I really do need a newer version for better results on this argument.Instead of "-grayscale Rec709Luminance", you could use "-colorspace Gray".
Thank you for all your help!
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Extract colors from image
Did you read the link I supplied?alasdairdf wrote:But is this identical or is "-grayscale Rec709Luminance" going to give better results?
Typically the linear Rec709Luminance formula is used, which is the same formula used when converting images to -colorspace gray.
snibgo's IM pages: im.snibgo.com