Replace color but only in some regions

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?".
cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Replace color but only in some regions

Post by cgkas » 2018-10-18T13:53:00-07:00

Hi, as part of another project I ask if is possible the following.

In this image below you see a green arc. Well, the 2 black rectangles and the small red rectangle have a red line across them. The lines and the arc have the same type of green.
Image


I ask if is possible to replace only the color of the arc from green to purple and the green lines within the 3 rectangles remain green like this:
Image

Thanks in advance for any help.

Bonzo
Posts: 2970
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Replace color but only in some regions

Post by Bonzo » 2018-10-18T14:16:49-07:00

Is this an actual image you are going to be using? Sometimes users post an image and the forum users reply and in fact it is nothing like the actual image the OP is going to use. This means that time is wasted with incorrect answers.

Are the other images you are going to use similar?

If this is the actual image and all the others are the same you could crop the top half, modify the colour then paste it back together again; but as you can guess this will not be the same for an image where the green arc was at the bottom.

cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Replace color but only in some regions

Post by cgkas » 2018-10-18T14:44:56-07:00

Hi Bonzo,

The green areas are not only on the top. The green lines I want to isolate have 1 pixel heigth. If there is way to identify these lines to change the colors only tho the lines to diferentiate from the rest of green areas, or replace colors of green areas except the 1 pixel lines.

A more representative input image is this:

Image

and output in this case like this:
Image

Thanks for any help.

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

Re: Replace color but only in some regions

Post by snibgo » 2018-10-18T15:17:09-07:00

Your second image has a PNG extension but is really a JPEG. This is lossy, which means some of your "green" pixels are really a different colour. Please don't post PNG images that are really JPEG. It confuses everyone.

What do you want to do about the antialiasing around the green arcs?

A way to approach the problem is:

1. Convert green pixels to white, and everything else to black.

2. From that, turn black all white pixels that have a black pixel above and below.

3. The result is now white where we want to replace green with purple, and black where we want no change.

4. So, we make an image with this change (or simply make a purple image) and do a masked composite over the input.
snibgo's IM pages: im.snibgo.com

cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Replace color but only in some regions

Post by cgkas » 2018-10-19T09:07:29-07:00

Hi snibgo,

Thanks for the ideas.

I was able to reach the step 3 of your suggestion, changing to purple only those regions I want and rest is black.

Now from here how can I do the mask to mix this purple and black image with the original input?

This is what I have so far:

Code: Select all

areas=$(convert input.png     \
-transparent '#4CFE00' \
-alpha extract -negate \
-threshold 50% -type bilevel \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-connected-components 4 \
out.png | awk '/x1\+/{print $2}')

for region in $areas; do
    convert out.png -region $region -fill '#000000' -colorize 100 out.png
done

convert out.png -fill "#7F006E" -opaque white out.png

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

Re: Replace color but only in some regions

Post by snibgo » 2018-10-19T09:39:22-07:00

Please post a link to input.png, and ensure it really is a PNG, not a JPEG.
snibgo's IM pages: im.snibgo.com

cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Replace color but only in some regions

Post by cgkas » 2018-10-19T10:07:13-07:00

I have saved as PNG. How can be sure not a JPEG?

User avatar
fmw42
Posts: 24004
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace color but only in some regions

Post by fmw42 » 2018-10-19T11:00:00-07:00

you can use

Code: Select all

identify -verbose image.png
it will tell you the proper format.

Or use dropbox.com, which does not change your image format.

cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Replace color but only in some regions

Post by cgkas » 2018-10-19T12:04:30-07:00

Attaching again input.png from Dropbox. I hope this time has the correct format.

Image
Last edited by cgkas on 2018-10-19T12:21:55-07:00, edited 1 time in total.

User avatar
fmw42
Posts: 24004
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace color but only in some regions

Post by fmw42 » 2018-10-19T12:12:45-07:00

That new link does download a PNG image.

cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Replace color but only in some regions

Post by cgkas » 2018-10-19T12:22:10-07:00

Attached again

User avatar
fmw42
Posts: 24004
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace color but only in some regions

Post by fmw42 » 2018-10-19T12:30:51-07:00

Yes that is PNG also.

cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Replace color but only in some regions

Post by cgkas » 2018-10-19T12:41:13-07:00

Yes. Snibgo told me to attach the input PNG ensuring that was not a JPEG. I'm confused what you need.

User avatar
fmw42
Posts: 24004
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace color but only in some regions

Post by fmw42 » 2018-10-19T12:44:20-07:00

I think that was all he needed so that he or you could properly use his code. But I will defer to him.

cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Replace color but only in some regions

Post by cgkas » 2018-10-19T13:39:55-07:00

I've been able to do the first 3 steps snibgo suggested. Now, I dont know how to do the last step. A masked composite over the input.

A way to approach the problem is:
1. Convert green pixels to white, and everything else to black.
2. From that, turn black all white pixels that have a black pixel above and below.
3. The result is now white where we want to replace green with purple, and black where we want no change.
4. So, we make an image with this change (or simply make a purple image) and do a masked composite over the input.

Post Reply