Page 1 of 1
Varying transparency according to rgb values
Posted: 2010-03-01T11:19:19-07:00
by weatherbadger
Hello
I am trying to convert my images (which have transparent background) varying the level of transparency of each pixel according to its rgb colour intensity. So lighter colors (with higher rgb values) will become more transparent and darker colors (lower rgb) will remain almost opaque.
So far my best stab is
Code: Select all
convert accumulatedprecip.png -alpha on -channel A -evaluate multiply .75 +channel transp_accumulatedprecip.png
But this changes the level of transparency of the whole image.
I've also tried
Code: Select all
convert accumulatedprecip10.png -channel rgba -fill "rgba(200,200,255,0.35)" -fuzz 20% -opaque "rgb(20,20,255)" transp.png
The images are overlaid a basemap but the darker colors still look washed out.
The results from my first attempt are here
http://www.weatherbadger.com/viewer.php?area=nweurope Hopefully you can see what I mean. I'm not sure if its actually possible to change transparency for individual rgb values though...
Any suggestions would be greatly appreciated.
Thanks
Re: Varying transparency according to rgb values
Posted: 2010-03-01T15:12:10-07:00
by snibgo
Does this do what you want?
Code: Select all
# make an image totally transparent where it was white, fading to totally opaque where it was black
convert input.png -alpha on +clone -alpha off -negate -compose copyOpacity -composite -negate output.png
Re: Varying transparency according to rgb values
Posted: 2010-03-01T15:39:22-07:00
by fmw42
Post links to your input image and basemap. I am not sure what you want to do with the original transparency. But if you just want to take an opaque image (no transparency) and make a transparency channel that varies with image intensity (brighter more transparent), then try
convert image.png \( +clone -colorspace gray -negate \) -alpha off -compose copy_opacity -composite result.png
Re: Varying transparency according to rgb values
Posted: 2010-03-01T15:53:54-07:00
by snibgo
I learn from the best. fmw's version uses brackets so we don't need that second negate. My version with brackets is:
convert input.png -alpha on \( +clone -alpha off -negate \) -compose copyOpacity -composite output.png
Re: Varying transparency according to rgb values
Posted: 2010-03-01T16:25:34-07:00
by fmw42
the other difference is that I converted his color image to grayscale intensity to use for the alpha channel. I am not sure what ramifications there will be when using the actual color image for the transparency. it may just take the red channel data.
but if the image has transparency already and you don't need to use it, then my version would need to turn it off as snibgo did, so
convert image.png \( +clone -alpha off -colorspace gray -negate \) -alpha off -compose copy_opacity -composite result.png
or
convert \( image.png -alpha off \) \( +clone -colorspace gray -negate \) -alpha off -compose copy_opacity -composite result.png
If you need to merge the old alpha channel with the new one, then let us know and we can provide a modification of the above.
Re: Varying transparency according to rgb values
Posted: 2010-03-01T17:00:13-07:00
by snibgo
The docs say:
Code: Select all
copy-* ... If the channel specified does not exist in the source image, (which can only happen for methods, 'copy-opacity' or 'copy-black') then it is assumed that the source image is a special grayscale channel image of the values to be copied.
But "-colorspace gray" is more explicit. I'll try to remember it.
Re: Varying transparency according to rgb values
Posted: 2010-03-01T17:05:55-07:00
by weatherbadger
Wow, in the time it took me to trial a new script and crash my computer (nothing to do with IM), reboot and have a cup of tea...
I think we're nearly there. Here is a smaller version of where I'm starting from:
With the first sets of code I get
Not quite right - black background.
When I take out a -negate I get
This looks promising.
However, I want the light rain (pale blue) to be nearly transparent and the heavy rain (dark blue) to be nearly opaque. This seems to be the other way around.
fmw42's latest: convert accumulatedprecip9.png \( +clone -alpha off -colorspace gray -negate \) -alpha off -compose copy_opacity -composite result_4.png gives
which I think is right, but with a black rather than transparent background. Can you get this so it's transparent rather than black. These images are then overlaid on basemaps in an animation as per my link in at the start of the post.
Assuming this is doable I'm getting slightly worried that my white snow (in the top left hand corner) will become completely transparent if this happens however. If only snow was green or something.
Cheers thus far.
PS I realise I should have been asking for opacity rather than transparency. Thanks for understanding.
I'll pick this up tomorrow...off to bed.
Re: Varying transparency according to rgb values
Posted: 2010-03-01T17:50:10-07:00
by snibgo
I suggest you look at images in a program such as Gimp that shows transparency against a chequered background, so you can see it. Then try to define your requirements.
Your starter image has a large area that is transparent black. (Gimp shows it as transparent. In Gimp, I anti-erase it, showing the RGB colour, which is black.)
Our scripts ignore the original transparency. It is black, so we made it opaque, as you originally asked for.
Perhaps you first want to lay your original on a white image, so fully transparent areas become white. Then apply your original request, or the inverse, if you want opacity instead of transparency. (Note that "copy-opacity" really means "copy the alpha channel, without worrying about whether it means transparency or opacity.)
I'm off to bed, too.
Re: Varying transparency according to rgb values
Posted: 2010-03-01T18:23:48-07:00
by fmw42
try this, it combines your original opacity with the new one (via multiply)
convert accumulatedprecip9.png \
\( -clone 0 -alpha extract \) \
\( -clone 0 -alpha off -colorspace gray -negate \) \
\( -clone 1 -clone 2 -compose multiply -composite \) \
-delete 1,2 -alpha off -compose copy_opacity -composite \
accumulatedprecip9_proc.png
Re: Varying transparency according to rgb values
Posted: 2010-03-02T16:54:25-07:00
by weatherbadger
snibgo/fmw42
Result! By making my originals with a white background I've got what I want (for now). Thanks!
I have been using GIMP for a while to check how the opacity looks. I now need to play more with the shading of the plots and work on getting white snow.
fmw42 - your last bit of code I couldn't get to work with an error: UnrecognizedAlphaChannelType `Extract'. I couldn't find out why this is but am not worried though. I'm setting a white background in the original as mentioned above and using one of your previous lines which works well.
Thanks for all your help. I've learnt some more about IM and am sure there's a lot more it can do for my weather charts/site.
I'll probably be posting again about the colour of my snow. I have an idea how to make it appear white but this will have to wait.
Cheers
Weatherbadger
Re: Varying transparency according to rgb values
Posted: 2010-03-02T17:28:23-07:00
by fmw42
fmw42 - your last bit of code I couldn't get to work with an error: UnrecognizedAlphaChannelType `Extract'. I couldn't find out why this is but am not worried though. I'm setting a white background in the original as mentioned above and using one of your previous lines which works well.
Your version of IM is too old for that option. I think that came out in IM 6.4.3-7.
But if you really need to do that, there is an older way to avoid using that. Let me know if you want the equivalent for an older IM.
What version of IM are you using?
convert -version
IM is now at 6.6.0-0! You are probably hundreds of versions behind.
Re: Varying transparency according to rgb values
Posted: 2010-03-03T00:07:18-07:00
by weatherbadger
I'm using OpenSuse 11.1 with Imagemagick 6.4.3 so that explains why your alpha extract didn't work.
Interestingly(ish) There isn't a newer version available of IM via normal upgrade of OpenSuse 11.1
OpenSuse 11.2 comes with 6.5.4-8 I think.
I'm aiming to upgrade to OpenSuse 11.2 when I get the time (!) but became rather distracted with sorting out my rain and snow and playing with IM.
I'll manage for now though - many thanks again.
I'll be back here soon I'm sure
WB