How to copy a PNG to another PNG alpha channel?

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?".
i73
Posts: 50
Joined: 2016-08-24T11:30:27-07:00
Authentication code: 1151

How to copy a PNG to another PNG alpha channel?

Post by i73 »

I'm looking to take one image and clone it to another images alpha channel. I have been looking around this place as well as the Examples and have only found Copy_Opacity but I think this only handles the alpha of the image and not the actual channel.

Any help would be great!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to copy a PNG to another PNG alpha channel?

Post by snibgo »

Most colour images have 3 channels, so I'm not clear what you want to do.

If both images have no alpha ("-alpha off"), the grayscale is copied to the alpha channel.
snibgo's IM pages: im.snibgo.com
i73
Posts: 50
Joined: 2016-08-24T11:30:27-07:00
Authentication code: 1151

Re: How to copy a PNG to another PNG alpha channel?

Post by i73 »

snibgo wrote:Most colour images have 3 channels, so I'm not clear what you want to do.

If both images have no alpha ("-alpha off"), the grayscale is copied to the alpha channel.
Thanks for help,
Yes I have a PNG with RGB and Alpha and I am trying to copy ImageA.PNG to ImageB.PNGs alpha channel.
Or merge those two images into one with ImageA.PNG in RGB channels and ImageB.png in Alpha channels. Is this possible with ImageMagick?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to copy a PNG to another PNG alpha channel?

Post by snibgo »

Assuming the images are the same size:

Code: Select all

convert imageA.png imageB.png -alpha off -compose CopyOpacity -composite out.png
... out.png will be given the colour channels from imageA.png, and the alpha from imageB.png.
snibgo's IM pages: im.snibgo.com
i73
Posts: 50
Joined: 2016-08-24T11:30:27-07:00
Authentication code: 1151

Re: How to copy a PNG to another PNG alpha channel?

Post by i73 »

snibgo wrote:Assuming the images are the same size:

Code: Select all

convert imageA.png imageB.png -alpha off -compose CopyOpacity -composite out.png
... out.png will be given the colour channels from imageA.png, and the alpha from imageB.png.
Thanks so much! This is in fact what I had before but I am missing color data on the RGB, is there anyway I can apply it to the Alpha Channel while keeping the RGB values in their own channel? Essentially, what I am trying to do is have 2 different images in one. I know it sounds weird but It's needed.

Image

Thanks for all your help!!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to copy a PNG to another PNG alpha channel?

Post by fmw42 »

Imagemagick only supports one alpha channel in IM 6. IM 7 does support more than one alpha channel, but you cannot write that out to any common image format. You can combine alpha channels from two images and apply that one alpha channel to either of the two images. Typically you extract both alpha channels, multiply (-compose multiply -composite) and then put that result into the alpha channel of either image using the command above that snibgo provided.
i73
Posts: 50
Joined: 2016-08-24T11:30:27-07:00
Authentication code: 1151

Re: How to copy a PNG to another PNG alpha channel?

Post by i73 »

fmw42 wrote:Imagemagick only supports one alpha channel in IM 6. IM 7 does support more than one alpha channel, but you cannot write that out to any common image format. You can combine alpha channels from two images and apply that one alpha channel to either of the two images. Typically you extract both alpha channels, multiply (-compose multiply -composite) and then put that result into the alpha channel of either image using the command above that snibgo provided.
Thanks! I'm not looking to combine alpha channels I am trying to take one image (RGB or Greyscale) and assign it to the alpha channel in the other image without applying pre multiply to the RGB of the first image.

Literally I am trying to assign 2 textures to one PNG by trying to use one textures alpha channel to hold the second textures grey scale.
In code it's something like this.

Code: Select all

texture ImageA; 
texture ImageB; 

resultImage.SetPixel(x, y, Color.FromArgb(ImageA.R, ImageB.R, ImageB.G, ImageB.B));
Wrote that in C# but it is pre multiplying the RGB with the alpha and is the exact same thing:

Code: Select all

convert D:\ImageA.png D:\ImageB.png -alpha off -compose CopyOpacity -composite D:\out.png
does and I don't want that
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to copy a PNG to another PNG alpha channel?

Post by fmw42 »

Lets say you want to put a grayscale version of image2 into the alpha channel of image1. The try

Unix:

Code: Select all

convert image1 \( image2 -colorspace gray \) -alpha off -compose copy_opacity -composite result
For windows, remove the \ before ( and before ).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to copy a PNG to another PNG alpha channel?

Post by snibgo »

I don't understand your comment about pre-multiplication. There is no pre-multiplication (by alpha, of the colour channel) in the command I gave.

Perhaps it is simpler for you to supply inputs, with the desired output.
snibgo's IM pages: im.snibgo.com
i73
Posts: 50
Joined: 2016-08-24T11:30:27-07:00
Authentication code: 1151

Re: How to copy a PNG to another PNG alpha channel?

Post by i73 »

snibgo wrote:I don't understand your comment about pre-multiplication. There is no pre-multiplication (by alpha, of the colour channel) in the command I gave.

Perhaps it is simpler for you to supply inputs, with the desired output.
From what I have been testing all day on c++, C# (BitMap, WriteableBitmap), ImageMagick I have found that PNG's (Either with .net or ImageMagick) take the alpha channel and multiply it with the RGB channels.

This is imageA.png:
Image


This is imageB.png
Image
ImageA.png (In this image I make a box in the Red layer)
ImageB.png (in this image I make stripes of RGB colors horizontally)

As you can see the ImageA.png is applied to the Alpha channel as well as RGB, what I am looking for is a way to not apply the Alpha (Pre multiply) the RGB channels with the Alpha channel so:
Channel R will only have horizontal stripes
Channel G will only have horizontal stripes
Channel B will only have horizontal stripes
Channel A will only have a box


Is it possible with your knowledge to do this with ImageMagick?

Thanks so much!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to copy a PNG to another PNG alpha channel?

Post by snibgo »

snibgo wrote:Perhaps it is simpler for you to supply inputs, with the desired output.
Sorry I wasn't clear. I meant for you to link to (or show) the actual input and desired output image files, not screen shots.

There is no pre-multiplying in the command I showed. When you use the result in some other operation, there may be pre-multiplying, of course. That's what the alpha channel is for. In v7, "-alpha discrete" may be useful.
snibgo's IM pages: im.snibgo.com
i73
Posts: 50
Joined: 2016-08-24T11:30:27-07:00
Authentication code: 1151

Re: How to copy a PNG to another PNG alpha channel?

Post by i73 »

snibgo wrote:
snibgo wrote:Perhaps it is simpler for you to supply inputs, with the desired output.
Sorry I wasn't clear. I meant for you to link to (or show) the actual input and desired output image files, not screen shots.
Ah, yes so I have attached exactly what I am looking for below.
This is the image that I made:
Image

And this is exactly what I want the Channels to look like:
Image

Notice how the Alpha channel is not being multiplied to the RGB channels, it has its own layer that is not effecting the RGB layers. Essentially what I am looking for is the Alpha channel to be its own texture.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to copy a PNG to another PNG alpha channel?

Post by fmw42 »

Your alpha channel is disabled, is it not, in PS. So it does not contribute to your output image. What do you then do with the alpha channel? If it is disabled, then you do not really need it, unless you intend to use it to mask the RGB result.

If you post your RGB image and your alpha channel as its own image, then we can make that into a PNG image in IM. But the alpha will be on by default. You can temporarily disable it in an IM command by using -alpha off, then reenable it in the same command later with -alpha on.

If this is not what you want, then please clarify further.

But it would help if you post your PSD image or the input images, so we can test with them. Not screen shots, but the actual images.

What we describe before will copy one grayscale image to another image's alpha channel. From there you can disable or enable it.


Perhaps I have missed it, but what is your IM version and platform? Please always provide that, since syntax may differ.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to copy a PNG to another PNG alpha channel?

Post by snibgo »

Your test1.png has no alpha channel. Is this one of the inputs, or the desired output?

You seem to have two inputs and a desired output from those inputs. Please show them or link to them.
snibgo's IM pages: im.snibgo.com
i73
Posts: 50
Joined: 2016-08-24T11:30:27-07:00
Authentication code: 1151

Re: How to copy a PNG to another PNG alpha channel?

Post by i73 »

fmw42 wrote: But it would help if you post your PSD image or the input images, so we can test with them. Not screen shots, but the actual images.
Giving you the image won't be of any use to you because I cannot save the Alpha so you cannot see the desired effect, but here they are:

The two test images that I try and copy Test.png to Test2.png Alpha channel: http://www.fast-files.com/getfile.aspx?file=120064

The output image that it gives me: http://www.321webs.com/download/120065.htm
-If you open that file in Gimp you can notice how the Alpha channel is a box and it's multiplied along all the RGB? I don't want that. I want the box to stay in the Alpha channel and the bars to stay in the RGB channels.

In C# and C++ the image is multiplying (Taking the alpha channel and applying it to the RGB channels) I don't want this, I am told that IM can do what I am looking to do, Literally I am trying to take ImageA.png and move it into the alpha channel in imageB.png without having ImageB.png RGB channels effected by it's Alpha channel.

I want to make a image with 4 channels:
Red: imageA.png -R
Green: imageA.png -G
Blue: imageA.png B
Alpha: imageB.png -RGB or Greyscale


I don't want transparency to be applied to RGB channels, I want the Alpha channel to act as a layer where I can place a image, 'essentially' making one png file hold two images one in the RGB another in the A.

I am told that IM can do this so don't shoot me if PNG's cannot support this type of thing. :P

Thanks so much guys,
Post Reply