grey (gray) alpha mask to get perfect 50% transparency

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
imagegem
Posts: 9
Joined: 2010-10-30T11:03:50-07:00
Authentication code: 8675308

grey (gray) alpha mask to get perfect 50% transparency

Post by imagegem »

Hello all

I am trying to get 50% transparency using an alpha mask. I have created a grey jpg for my alpha that uses rgb 127 values. I am using this technique:

convert ORIGINAL.JPG -background none -alpha Off GREY.JPG -compose CopyOpacity -composite PNG32:IMAGE_ALPHA.PNG

But the grey i use is not creating 50% transparency. it is creating something like 70% transparency. Because if i run the command line twice, and produce two versions, let's say the identical IMAGE_ALPHA_v1.PNG and IMAGE_ALPHA_v2.PNG, then recombine them, to form RECOMBINDED.PNG, the recombined does not match the orginal. It is lighter than the original and still has some transparency in it.

I am combining like this
composite -dissolve 100x100 -gravity center IMAGE_ALPHA_v1.png IMAGE_ALPHA_v2.png RECOMBINDED.PNG.png

If i take that grey alpha jpg, and make it about 75% grey (eyeball method), it works. the recombined matches the original. So it must have something to do with the grey ramp that i use to create grey.jpg

i have tried other methods of creating 50% grey, eg 50% brightness in hsb, or 100% black with 50% transparency on the pixels. I've also tried L values from LAB. Does anyone know what IM uses to create 50% transparency. what rgb or hsb values can i use. I'm actually looking for the whole ramp, 0-100%. Erm, 0% and 100% are working as expected!

I hope my explanation makes sense.

Thanks in advance for anyone who knows the grey ramp answer or the correct technique.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: grey (gray) alpha mask to get perfect 50% transparency

Post by snibgo »

It's easier to diagnose problems if you tell us the IM version, your platform, and supply your images. Or at any rate, tell us how you created GREY.JPG. You may be hitting the infamous RGB/sRGB issue.

If you just want your entire image to have a certain transparency (like 50%), the easiest method is:

Code: Select all

convert original.jpg -alpha set -channel Alpha -evaluate set 50% out.png
(If you are using a Windows bat file, don't forget to double the "%".)
snibgo's IM pages: im.snibgo.com
imagegem
Posts: 9
Joined: 2010-10-30T11:03:50-07:00
Authentication code: 8675308

Re: grey (gray) alpha mask to get perfect 50% transparency

Post by imagegem »

thanks for taking the time to help. silly me regarding the IM version etc. I'm am using 6.8.0-7 from macports.

Unfortunately I can't use the 50% transparency you suggest, because what I'm actually trying to do is create a black-grey-white graduated fade to be used as the alpha.

For example on image A the graduated alpha will be used left to right, and on image B it will be used right to left. I then make whatever corrections i have to do on the two output images, and recombine. So the 50% in the middle recombining perfectly is only half the problem. The 25% transparency from one output needs to recombine perfectly with the 75% transparency from the other output. etc.

my "50% solid grey" that i'm doing me testing on is before I move on to the more complicated graduated fade.

Anyhoo, for example downloading this image
http://www.imagemagick.org/image/wizard.jpg

make gray at same size as wizard.jpg
convert -size 265x352 xc:'gray(50%)' grey.jpg

make 1st version with 50% transparency
convert wizard.jpg -background none -alpha Off grey.jpg -compose CopyOpacity -composite PNG32:image_alpha1.png

make 2nd version with 50% transparency
convert wizard.jpg -background none -alpha Off grey.jpg -compose CopyOpacity -composite PNG32:image_alpha2.png

recombine them
composite -dissolve 100x100 -gravity center image_alpha1.png image_alpha2.png combined.png






snibgo wrote:It's easier to diagnose problems if you tell us the IM version, your platform, and supply your images. Or at any rate, tell us how you created GREY.JPG. You may be hitting the infamous RGB/sRGB issue.

If you just want your entire image to have a certain transparency (like 50%), the easiest method is:

Code: Select all

convert original.jpg -alpha set -channel Alpha -evaluate set 50% out.png
(If you are using a Windows bat file, don't forget to double the "%".)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: grey (gray) alpha mask to get perfect 50% transparency

Post by snibgo »

Your final assembly is weird. I think you want the effect of one picture placed over another, where the one on top varies in transparency, perhaps from fully transparent to entirely opaque.

If your bottom picture is also (partially) transparent in some areas, then in those places where both images have transparency, the result will also have transparency.

Try this ...

Code: Select all

bottom.png top.png mask.png -composite result.png
... where mask.png is entirely gray, or fades from black to white, or whatever. But it should be in RGB colorspace.

For example:

Code: Select all

convert -size 100x100 xc:red xc:green gradient: -set colorspace RGB -composite -set colorspace sRGB x.png
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: grey (gray) alpha mask to get perfect 50% transparency

Post by fmw42 »

Have you tried

convert ORIGINAL.JPG -alpha set -channel a -evaluate set 50% +channel PNG32:IMAGE_ALPHA.PNG
imagegem
Posts: 9
Joined: 2010-10-30T11:03:50-07:00
Authentication code: 8675308

Re: grey (gray) alpha mask to get perfect 50% transparency

Post by imagegem »

fmw42 wrote:Have you tried

convert ORIGINAL.JPG -alpha set -channel a -evaluate set 50% +channel PNG32:IMAGE_ALPHA.PNG

fmw42, thank you, that would work for a flat, constant 50%, but I need to create graduated fades as masks. My example was just trying to solve 50% for convenience. I need to solve for every percent!

However the comment earlier about the gamma problem has led me on to something which might be the answer. when I create my greyscale graduated fade, if I apply a gamma of 1/2.2 to every colour while drawing my graduated fade, it seems to be coming out right. Still doing a bit more tests, but this might be the right solution.

snibgo, if I have an pixel with 50% transparency, and overlay another pixel with 50% transparency, how do i add the opaqueness to get rid of that transparency?

eg bottom pixel 50% transparent (ergo 50% opaque)
top pixel 50% transparent (ergo 50% opaque)
result i want: 0% transparent, 100 opaque.

Thanks in advance...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: grey (gray) alpha mask to get perfect 50% transparency

Post by fmw42 »

grayscale was changed to linear (darker) at 6.7.8.3 so you need to add -set colorspace RGB before converting to grayscale from a color image to keep it non-linear gray (lighter). Or if creating a gradient you need to use gradient: or gradient:"white-black" or gradient:"gray100-gray0" rather than gray(...) to get nonlinear gray. The gray(0) to gray(255) or gray(xx%) are linear gray and would need to be convert from linear gray to non-linear gray, I believe, to work properly.

What command did you use to create your alpha channel image?

If it was converted from some color image, try

convert image -set colorspace RGB -colorspace gray result

If creating a gradient

convert -size WxH gradient: result
imagegem
Posts: 9
Joined: 2010-10-30T11:03:50-07:00
Authentication code: 8675308

Re: grey (gray) alpha mask to get perfect 50% transparency

Post by imagegem »

cool, thanks for help, i have got it to work like this

make wizard jpg
curl http://www.imagemagick.org/image/wizard.png > wizard.png
convert wizard.png wizard.jpg

make 1 gradient
convert -size 1468x1104 gradient:'white-black' -rotate 90 -set colorspace RGB -colorspace gray -gamma 2.2 r2l.jpg

make 2 gradient
convert -size 1468x1104 gradient:'black-white' -rotate 90 -set colorspace RGB -colorspace gray -gamma 2.2 l2r.jpg

combine 1 gradient with pic
convert wizard.jpg -background none -alpha Off r2l.jpg -compose CopyOpacity -composite PNG32:image_alpha_r2l.png

combine 2 gradient with pic
convert wizard.jpg -background none -alpha Off l2r.jpg -compose CopyOpacity -composite PNG32:image_alpha_l2r.png

in theory overlaying these two will return me to my original image
composite -dissolve 100x100 -gravity center image_alpha_r2l.png image_alpha_l2r.png combined.png

..and convert to jpeg, which looks almost identical to where i started
convert combined.png combined.jpg

combined.jpg and wizard.jpg are the same, hooray!

thank you for your help. You may hear from me again if i'm celebrating too early!


fmw42 wrote:grayscale was changed to linear (darker) at 6.7.8.3 so you need to add -set colorspace RGB before converting to grayscale from a color image to keep it non-linear gray (lighter). Or if creating a gradient you need to use gradient: or gradient:"white-black" or gradient:"gray100-gray0" rather than gray(...) to get nonlinear gray. The gray(0) to gray(255) or gray(xx%) are linear gray and would need to be convert from linear gray to non-linear gray, I believe, to work properly.

What command did you use to create your alpha channel image?

If it was converted from some color image, try

convert image -set colorspace RGB -colorspace gray result

If creating a gradient

convert -size WxH gradient: result
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: grey (gray) alpha mask to get perfect 50% transparency

Post by snibgo »

eg bottom pixel 50% transparent (ergo 50% opaque)
top pixel 50% transparent (ergo 50% opaque)
result i want: 0% transparent, 100 opaque.
Make the bottom pixel opaque. Job done.
snibgo's IM pages: im.snibgo.com
imagegem
Posts: 9
Joined: 2010-10-30T11:03:50-07:00
Authentication code: 8675308

Re: grey (gray) alpha mask to get perfect 50% transparency

Post by imagegem »

snibgo wrote:
eg bottom pixel 50% transparent (ergo 50% opaque)
top pixel 50% transparent (ergo 50% opaque)
result i want: 0% transparent, 100 opaque.
Make the bottom pixel opaque. Job done.
Thanks Snibgo and all others who helped here.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: grey (gray) alpha mask to get perfect 50% transparency

Post by fmw42 »

In principle this should be sufficient:

convert -size 1468x1104 gradient:'white-black' -rotate 90 [or 270] result
or even
convert -size 1468x1104 gradient: -rotate 90 [or 270] result



all the colorspace stuff following the creation of the gradient is just confusing thing so that you need t use -gamma.
Post Reply