Page 1 of 1

Applying 8-bit alpha to RGB image

Posted: 2013-01-15T13:17:56-07:00
by astrolabe
Hello,

I use ImageMagick a fair bit, but usually I have to stumble around until I happen to find something that works.

I am trying to use 8-bit grayscale images to apply an 8-bit alpha mask to 24-bit RGB images. After a lot of searching, the best I've managed is:

convert RGB_IMAGE.png -alpha copy GRAY_MASK.png -alpha off -compose copy_opacity -composite RESULT.png

Most of this appears to work, but it gives me an 8-bit grayscale RESULT.png, instead of 24-bit color.

If I start with a grayscale source image, it does what I would expect, but color source images don't work. The simple examples in the help pages seem to give me 1-bit alpha.

Can someone tell me what I'm leaving out?

Thanks

Re: Applying 8-bit alpha to RGB image

Posted: 2013-01-15T14:18:49-07:00
by snibgo
What is "-alpha copy" doing there? What do you expect it to do? I think that's the problem. The rest of it looks fine.

Code: Select all

convert RGB_IMAGE.png GRAY_MASK.png -alpha off -compose copy_opacity -composite RESULT.png
The result should contain the colours of RGB_IMAGE.png, but with an alpha channel. Where GRAY_MASK.png is black, the result will be transparent. Where GRAY_MASK.png is white, the result will be opaque. And grey will be semi-transparent.

If this isn't what you get, you might link to the three files.

Re: Applying 8-bit alpha to RGB image

Posted: 2013-01-15T15:08:56-07:00
by astrolabe
Thanks for your reply

I think -alpha copy is extracting the alpha from GRAY_MASK.png? When I used -alpha extract I got 1-bit alpha. This started from a command string that I found in another forum. As I say, my understanding is rather murky.

A simple example of the files is:
convert RGB_IMAGE.png -alpha copy GRAY_MASK.png -alpha off -compose copy_opacity -composite RESULT.png

Image + Image --> Image

What I want is:
Image

There is 8-bit transparency here.

The command line that you recommend gives me an 8-bit color image with one transparent color (like a GIF).

Re: Applying 8-bit alpha to RGB image

Posted: 2013-01-15T18:36:26-07:00
by snibgo
Ah, okay. You need to enable alpha with "-alpha set":

Code: Select all

convert rgb_image.png -alpha set gray_mask.png -alpha off -compose copy_opacity -composite r.png
For a brief explanation of commands, see http://www.imagemagick.org/script/comma ... ptions.php

Re: Applying 8-bit alpha to RGB image

Posted: 2013-01-15T19:27:26-07:00
by astrolabe
That still gives me an 8-bit image with 1-bit alpha:

Image

You have to zoom in to the edge of the circle, but both RESULT.png and desired_result.png have 8-bit transparency.

Re: Applying 8-bit alpha to RGB image

Posted: 2013-01-15T19:53:53-07:00
by snibgo
It gave me 8-bit transparency, with partial transparency showing at the edge of the circle. (IM 6.7.9 on Windows 7.)

Re: Applying 8-bit alpha to RGB image

Posted: 2013-01-15T20:26:09-07:00
by astrolabe
I'm using v6.8.0-5 on Win XP SP3. I'll try updating IM, but it's fairly current.

Re: Applying 8-bit alpha to RGB image

Posted: 2013-01-16T12:53:07-07:00
by astrolabe
The current version ImageMagick-6.8.1-9-Q16-x86-dll.exe also does it incorrectly: 8-bit color + 1 bit alpha.

ImageMagick-6.6.3-1-Q16-windows-dll.exe does what it is supposed to do: 24-bit color + 8 bit alpha.

The command line is:

convert rgb_image.png -alpha set gray_mask.png -alpha off -compose copy_opacity -composite r_(v6.6.3).png

Image


v6.6.3 is the most recent older version that I have. The 'legacy' directories on the servers listed at http://www.imagemagick.org/ only seem to contain the source code for the older versions. Is there an archive of the old windows binaries somewhere?

Re: Applying 8-bit alpha to RGB image

Posted: 2013-01-16T13:06:55-07:00
by fmw42
does it work any different with

convert rgb_image.png gray_mask.png -alpha off -compose copy_opacity -composite result.png

No need to turn on alpha on the input image if you are going to turn it off any way before putting the second image into the alpha channel.

Is your rgb_image.png 8-bit or 24-bit?

Is your mask image 8-bit or 1-bit? Is it grayscale or color?

also try


convert rgb_image.png gray_mask.png -alpha off -compose copy_opacity -composite PNG32:result.png

Re: Applying 8-bit alpha to RGB image

Posted: 2013-01-16T14:00:37-07:00
by astrolabe
I wondered if there was come format specification that I needed (e.g. PNG32).

RGB_IMAGE.png is 24-bit color, no alpha
GRAY_MASK.png is 8-bit grayscle, no alpha
RESULT.png should be 24-bit color, 8-bit alpha (32-bit)

IM v6.6.3 gives the correct result with both command lines you listed.

with v6.8.1-9,
convert rgb_image.png gray_mask.png -alpha off -compose copy_opacity -composite result.png
gives 8-bit color with 1-bit alpha

convert rgb_image.png gray_mask.png -alpha off -compose copy_opacity -composite PNG32:result.png
gives the correct result: 24-bit color, 8-bit alpha

I guess the 24-bit color, 8-bit alpha format has to be specifically defined in the current version, rather than assumed as in the IM v6.6.3 version.

A lot of the nuances are beyond my current level of understanding of the program, but I would expect the default format here to be the 32-bit format, with the indexed 8-bit color requiring an explicit option, rather than the way it is now. I started with a 24-bit color and 8-bit alpha files, so I would expect a 32-bit result by default.

At any rate, thanks for a solution.

Re: Applying 8-bit alpha to RGB image

Posted: 2013-01-16T14:19:48-07:00
by snibgo
RGB_IMAGE.png is 24-bit colour. However, all the pixels are the same colour. I suppose IM realises this and tries to use a more space-efficient format, which is fair enough. However, the copy_opacity has created an 8-bit alpha, with a number of different values. IM shouldn't try to reduce this to one bit. If it is doing so, I'd call that a bug.

Re: Applying 8-bit alpha to RGB image

Posted: 2013-01-22T18:41:26-07:00
by anthony
PNG32 has a bit depth of 8-bit... BY DEFINITION. That is 8 bits of red,green,blue and alpha.

So save to PNG32:result.png