Extract solid color as alpha

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
vincent_Westimages
Posts: 2
Joined: 2014-11-24T06:44:56-07:00
Authentication code: 6789

Extract solid color as alpha

Post by vincent_Westimages »

Hi,

How can i extract all solid colors from a picture (7 or 8 colors max) and export each color as a separate image with transparency on non-color parts?
I've searched the forum but i'm really new with image magick.
A picture is worth a thousand words, so.. Image

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

Re: Extract solid color as alpha

Post by snibgo »

I would do it in a script. First, extract the unique-colours as text. Loop through these colours, making all pixels except the chosen colour transparent.

Something like this pseudo-code:

Code: Select all

convert in.png -unique-colors txt:colours.lis

for each colour in colours.lis
  convert in.png +transparent this_colour out_this_colour.png
end_for
For an extra bonus, you could do all the transparencies in one convert command.
snibgo's IM pages: im.snibgo.com
vincent_Westimages
Posts: 2
Joined: 2014-11-24T06:44:56-07:00
Authentication code: 6789

Re: Extract solid color as alpha

Post by vincent_Westimages »

Thanks a lot for this clarification,
I can now generate the colour.list and apply the transparency.
But once the colours.list is created , how can i loop in the .list to extract each color?
I mean , instead of entering each hexacode to extract the color , is it possible to use directly the .list?

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

Re: Extract solid color as alpha

Post by snibgo »

vincent_Westimages wrote:how can i loop in the .list to extract each color?
Your script would do this. Script languages include bash and Windows BAT.
snibgo's IM pages: im.snibgo.com
Post Reply