Applying 8-bit alpha to RGB image

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
astrolabe
Posts: 6
Joined: 2013-01-15T12:42:27-07:00
Authentication code: 6789

Applying 8-bit alpha to RGB image

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Applying 8-bit alpha to RGB image

Post 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.
snibgo's IM pages: im.snibgo.com
astrolabe
Posts: 6
Joined: 2013-01-15T12:42:27-07:00
Authentication code: 6789

Re: Applying 8-bit alpha to RGB image

Post 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).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Applying 8-bit alpha to RGB image

Post 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
snibgo's IM pages: im.snibgo.com
astrolabe
Posts: 6
Joined: 2013-01-15T12:42:27-07:00
Authentication code: 6789

Re: Applying 8-bit alpha to RGB image

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Applying 8-bit alpha to RGB image

Post 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.)
snibgo's IM pages: im.snibgo.com
astrolabe
Posts: 6
Joined: 2013-01-15T12:42:27-07:00
Authentication code: 6789

Re: Applying 8-bit alpha to RGB image

Post by astrolabe »

I'm using v6.8.0-5 on Win XP SP3. I'll try updating IM, but it's fairly current.
astrolabe
Posts: 6
Joined: 2013-01-15T12:42:27-07:00
Authentication code: 6789

Re: Applying 8-bit alpha to RGB image

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Applying 8-bit alpha to RGB image

Post 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
astrolabe
Posts: 6
Joined: 2013-01-15T12:42:27-07:00
Authentication code: 6789

Re: Applying 8-bit alpha to RGB image

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Applying 8-bit alpha to RGB image

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Applying 8-bit alpha to RGB image

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply