Convert grayscale part of image to color

A plethora of command-line scripts that perform geometric transforms, blurs, sharpens, edging, noise removal, and color manipulations.
Post Reply
anodyne
Posts: 3
Joined: 2016-01-06T06:33:24-07:00
Authentication code: 1151

Convert grayscale part of image to color

Post by anodyne »

Sorry if this has been covered before. I'm using modulatecolor2 to change specific colors of an icon to another one and it's working great.

What I still need to do though is change the remaining grayscale portion to another color (e.g. blue). Is something like this possible?

Here's an example of one of these images.

http://s1.postimg.org/h5cywqq8b/color_swap.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert grayscale part of image to color

Post by fmw42 »

Best I can suggest is to follow with

Code: Select all

convert color_swap.png.jpeg.png -alpha off -fuzz 20% -fill blue +opaque "#80C145" -alpha on result.png
anodyne
Posts: 3
Joined: 2016-01-06T06:33:24-07:00
Authentication code: 1151

Re: Convert grayscale part of image to color

Post by anodyne »

Thanks for your response Fred. That worked, but is there a way to throw transparency on that fill? When I try something like below, I get the intended fill color, but there's no transparency and the fill ends up filling in both the white and gray. Any ideas?

Code: Select all

convert color-swap.png -alpha off -fuzz 20% -fill 'rgba(0, 0, 255, 0.8)' +opaque "#80C145" -alpha on result.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert grayscale part of image to color

Post by fmw42 »

This is a bit more tricky. You have to save the original alpha channel. Then do your processing. Then extract the new alpha channel and multiply it by the old alpha channel. Then delete the temp files and put the product alpha channel into the processed image.

Code: Select all

convert color_swap.png \
\( -clone 0 -alpha extract \) \
\( -clone 0 -alpha off -channel rgba -fuzz 20% \
-fill "rgba(0,0,255,0.8)" +opaque "#80C145" \) \
\( -clone 2 -alpha extract \) \
\( -clone 1 -clone 2 -compose multiply -composite \) \
-delete 0,1,3 \
-alpha off -compose over -compose copy_opacity -composite result.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert grayscale part of image to color

Post by fmw42 »

It may be better to change the gray directly to transparent blue. I am not sure if what I gave you above is what you want or not. It may be reducing the alpha value too much (too transparent). So try this:

Code: Select all

convert color_swap.png -alpha on -channel rgba -fuzz 15% -fill "rgba(0,0,255,0.8)" -opaque "#D6D6D6" result.png
anodyne
Posts: 3
Joined: 2016-01-06T06:33:24-07:00
Authentication code: 1151

Re: Convert grayscale part of image to color

Post by anodyne »

Nice, looks like this last snippet should get me there. Thanks so much for your help!
Post Reply