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

Remove colors of an image one by one

Post by BigNoub » 2019-01-11T04:51:48-07:00

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=`./dominantcolor.sh -m 1 temp_"$i".jpg`	
        let "j = i + 1"
	./replacecolor.sh -i "$main_color" -o white temp_"$i".jpg temp_"$j".jpg 
done
This is temp_0.jpg

Image

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

Image

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]

snibgo
Posts: 12424
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 » 2019-01-11T06:08:31-07:00

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: im.snibgo.com

Post Reply