Replace color but only in some regions
Replace color but only in some regions
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.
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:
Thanks in advance for any help.
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.
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:
Thanks in advance for any help.
Re: Replace color but only in some regions
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.
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.
Re: Replace color but only in some regions
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:
and output in this case like this:
Thanks for any help.
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:
and output in this case like this:
Thanks for any help.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Replace color but only in some regions
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.
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
Re: Replace color but only in some regions
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:
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Replace color but only in some regions
Please post a link to input.png, and ensure it really is a PNG, not a JPEG.
snibgo's IM pages: im.snibgo.com
Re: Replace color but only in some regions
I have saved as PNG. How can be sure not a JPEG?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Replace color but only in some regions
you can use
it will tell you the proper format.
Or use dropbox.com, which does not change your image format.
Code: Select all
identify -verbose image.png
Or use dropbox.com, which does not change your image format.
Re: Replace color but only in some regions
Attaching again input.png from Dropbox. I hope this time has the correct format.
Last edited by cgkas on 2018-10-19T12:21:55-07:00, edited 1 time in total.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Replace color but only in some regions
That new link does download a PNG image.
Re: Replace color but only in some regions
Attached again
Re: Replace color but only in some regions
Yes. Snibgo told me to attach the input PNG ensuring that was not a JPEG. I'm confused what you need.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Replace color but only in some regions
I think that was all he needed so that he or you could properly use his code. But I will defer to him.
Re: Replace color but only in some regions
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.
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.