Remove colors of an image one by one

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?".
Posts: 33
Joined: 2015-12-29T14:49:52-07:00
Authentication code: 1151

Remove colors of an image one by one

Post by BigNoub »

I'm trying to remove the colors of an image one by one (greys and black included in what I mean by colors), as if I was making layers invisible one by one in Photoshop.

I tried to use Fred's scripts dominantcolor and replacecolor in a loop:

(original image temp_0.jpg)

Code: Select all

for i in 0 1 2 3 4 5; do
	main_color=`./ -m 1 temp_"$i".jpg`	
        let "j = i + 1"
	./ -i "$main_color" -o white temp_"$i".jpg temp_"$j".jpg 
This is temp_0.jpg


It works well for the first step, temp_1.jpg:


but then I'm unable to remove the greys and blacks, all images look similar to temp_1.jpg (with a few more white pixels). I'm not sure if it's because dominantcolor can not distinguish between the greys or because replacecolor can not find the specified grey colors, but I'm stuck there.

[Edit: if you think I should specify more the steps I need, let's say I want to delete all colors, then delete all grays, then delete all pure black. That would already be nice. Deleting the grays in 2 steps would be nicer but not absolutely necessary]

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

Re: Remove colors of an image one by one

Post by snibgo »

You save each result to JPG. But that is a lossy format, so some of your replaced colours won't be what you expect. I suggest you use a lossless format such as PNG (which will be slower) or tiff (not with jpeg compression) or miff.
snibgo's IM pages: