[SOLVED] Multiplying color by alpha
[SOLVED] Multiplying color by alpha
I've been struggling trying to figure out a command line which will let me to create premultiplied alpha images out of my PNGs, can someone help me out please?
The process I'm after is:
Take input image (PNG)
Multiply each pixel color value by the pixel alpha value
Output PNG with the modified colors and the original alpha
Cheers, Noel
The process I'm after is:
Take input image (PNG)
Multiply each pixel color value by the pixel alpha value
Output PNG with the modified colors and the original alpha
Cheers, Noel
Last edited by noggs on 2013-05-24T02:04:43-07:00, edited 3 times in total.
Re: Multiplying color by alpha
Compose the image over a black canvas, then copy the alpha channel into the output:
(probably should write to out.somethingelse because the output isn't really a PNG if it contains premultiplied colors!)
Code: Select all
convert -size WxH xc:black -alpha on -draw "image over 0,0 0,0 in.png" -draw "image copy-opacity 0,0 0,0 in.png" out.png
- GreenKoopa
- Posts: 457
- Joined: 2010-11-04T17:24:08-07:00
- Authentication code: 8675308
Re: Multiplying color by alpha
There are many ways to do this. Even glennrp's solution of composing on a black background could be done without -size and -draw. Here was my first thought:
Code: Select all
convert in.png ( +clone -alpha Extract ) -channel RGB -compose Multiply -composite out.png
Re: Multiplying color by alpha
Thanks both of you - I appreciate avoiding the -size option!
If I wanted to do some other operation, and as a final step do this, how would I set that up. For instance (note resize is not always happening so I may not know the size up front):
convert in.png -resize 128x128! -blur 1 [now apply the premultiply operations] out.png
Or would I be better writing this to a temporary image then running the premult as a separate pass?
If I wanted to do some other operation, and as a final step do this, how would I set that up. For instance (note resize is not always happening so I may not know the size up front):
convert in.png -resize 128x128! -blur 1 [now apply the premultiply operations] out.png
Or would I be better writing this to a temporary image then running the premult as a separate pass?
- GreenKoopa
- Posts: 457
- Joined: 2010-11-04T17:24:08-07:00
- Authentication code: 8675308
Re: Multiplying color by alpha
I believe my operation could simply be pasted as the final step, but only in the case of working with a single image.
- GreenKoopa
- Posts: 457
- Joined: 2010-11-04T17:24:08-07:00
- Authentication code: 8675308
Re: Multiplying color by alpha
An alternative using glennrp's thinking, using different operators. This is an intuitive approach because most people don't want to keep the alpha channel after it is used
but we simply copy it back in.
Here is the same thing, but with only reading in the image once. This is not only more efficient, it allows you to paste it into a longer set of operations.
This should yield the same result as my other solution. I don't know which may be better for you.
Code: Select all
convert in.png -background black -alpha Remove out.png
Code: Select all
convert in.png -background black -alpha Remove in.png -compose Copy_Opacity -composite out.png
Code: Select all
convert in.png -write mpr:temp -background black -alpha Remove mpr:temp -compose Copy_Opacity -composite out.png
Re: Multiplying color by alpha
Fantastic! The last command line lets me chain additional options and works great.
Your help is much appreciated!
Your help is much appreciated!
Re: [SOLVED] Multiplying color by alpha
Sorry to resurrect this but if I run the command on an image without an alpha channel it gets screwed up (if it's 24 bit it seems to get alpha'd by maybe 50%, if it's 8-bit paletised it went super bright and also very alpha'd).
Is it possible to change the command line to handle this case and not do any multiplication or create an alpha channel?
Alternatively maybe the command line could be changed to promote all input images to full 32 bit?
Is it possible to change the command line to handle this case and not do any multiplication or create an alpha channel?
Alternatively maybe the command line could be changed to promote all input images to full 32 bit?
- GreenKoopa
- Posts: 457
- Joined: 2010-11-04T17:24:08-07:00
- Authentication code: 8675308
Re: Multiplying color by alpha
Sure, we can make this work for a new case.
For the second solution I offered: First we handle the multiply, then we use Copy_Opacity to add the alpha channel back in. Copy_Opacity copies the alpha channel if there is one, but otherwise it uses the image itself, grayscaled, as the alpha channel. Instead of copying the alpha back into the original image, we could copy the newly multiplied RGB channels back into the original image, leaving the alpha, present or not, alone.
For the first solution I offered: we didn't copy the alpha channel back in, we modified the RGB channels in place. So I believe it should work in this new case. If it doesn't, let me know.
I never work with palette images, so if they act differently someone else will have to help. For a color modification such as this, I can imagine operating on the palette directly, making this very fast regardless of image size.
For the second solution I offered:
Code: Select all
convert in.png -write mpr:temp -background black -alpha Remove mpr:temp -compose Copy_Opacity -composite out.png
For the first solution I offered:
Code: Select all
convert in.png ( +clone -alpha Extract ) -channel RGB -compose Multiply -composite out.png
I never work with palette images, so if they act differently someone else will have to help. For a color modification such as this, I can imagine operating on the palette directly, making this very fast regardless of image size.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Multiplying color by alpha
Palette images only support binary transparency. So what you need to do is save to PNG32:out.png to make it keep it as 32-bit result.I never work with palette images
Re: Multiplying color by alpha
Ok that explains why the 24 bit images become transparent!GreenKoopa wrote:First we handle the multiply, then we use Copy_Opacity to add the alpha channel back in. Copy_Opacity copies the alpha channel if there is one, but otherwise it uses the image itself, grayscaled, as the alpha channel. Instead of copying the alpha back into the original image, we could copy the newly multiplied RGB channels back into the original image, leaving the alpha, present or not, alone.
It seems I'm not going to be able to find a command line which will effectively ignore 8bit or 24bit but do premultiplication on 32 bit PNGs. I can detect the image type during the build process and skip the extra command line options.
Thanks again guys!
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Multiplying color by alpha
Possibly: insert "-alpha set" immediately after the input file. If the file has no alpha, alpha is created as fully opaque. If it has one, this makes no change.
snibgo's IM pages: im.snibgo.com
Re: Multiplying color by alpha
YES! That's nailed it.
Thanks very much!
Thanks very much!