Append second alpha channel to .tif file or swap channel data

Hello, sorry I’m new to this software. I have this use case where I need to create a .tif file with 2 alpha channels. I’ve managed to create one using ImageMagick, but I need the channel order to be swapped around. Currently it’s denoted as ExtraSamples (2 0) (associated and unspecified) but I need them to be denoted as (0 2).

I was wondering if this software would allow a way to swap the channel (meta)data or if it would allow me to append a channel to an image so it’s denoted as how I specified in result of doing that.

The reason as to why I need these two alpha channels is that I need the end result to open like this in photoshop:
Holding both a pre-multiplied alpha and a separate Alpha Channel called “Alpha 1”.

Some example files:
example .tif with 1 alpha channel(pre-multiplied)
test3.tif (739.2 KB)

Example .tif with 2 alpha channels but in the incorrect order:
output-alpha2.tif (571.0 KB)

I’d also appreciate possible references to other softwares which might be able of giving me the desired result. Thank you in advance!

In G’MIC command line (cli), split will array all the channels into a series of single channel grayscales. append then can pull them back together after you rearrange your channels as you see fit.

[gmic]-0./ Start G'MIC interpreter.
[gmic]-0./ Input all frames of TIFF file 'output-alpha2.tif' at position 0 (1 image 4096x4096x1x5).
[gmic]-1./ Split image [0] along the 'c'-axis.
[gmic]-5./ Display images [0,1,2,3,4] = 'output-alpha2.tif, (...), output-alpha2_c4.tif'.
[0] = 'output-alpha2.tif':
  size = (4096,4096,1,1) [64 Mio of float32].
  data = (0,0,0,0,0,0,0,0,0,0,0,0,(...),0,0,0,0,0,0,0,0,0,0,0,0).
  min = 0, max = 65535, mean = 41.4099, std = 1435.78, coords_min = (0,0,0,0), coords_max = (1398,1651,0,0).
[1] = 'output-alpha2_c1.tif':
  size = (4096,4096,1,1) [64 Mio of float32].
  data = (0,0,0,0,0,0,0,0,0,0,0,0,(...),0,0,0,0,0,0,0,0,0,0,0,0).
  min = 0, max = 57311, mean = 32.2771, std = 1133.41, coords_min = (0,0,0,0), coords_max = (1961,1107,0,0).
[2] = 'output-alpha2_c2.tif':
  size = (4096,4096,1,1) [64 Mio of float32].
  data = (0,0,0,0,0,0,0,0,0,0,0,0,(...),0,0,0,0,0,0,0,0,0,0,0,0).
  min = 0, max = 57311, mean = 35.9718, std = 1265.64, coords_min = (0,0,0,0), coords_max = (1961,1107,0,0).
[3] = 'output-alpha2_c3.tif':
  size = (4096,4096,1,1) [64 Mio of float32].
  data = (0,0,0,0,0,0,0,0,0,0,0,0,(...),0,0,0,0,0,0,0,0,0,0,0,0).
  min = 0, max = 65535, mean = 13899.9, std = 26784.6, coords_min = (0,0,0,0), coords_max = (1962,1108,0,0).
[4] = 'output-alpha2_c4.tif':
  size = (4096,4096,1,1) [64 Mio of float32].
  data = (0,0,0,0,0,0,0,0,0,0,0,0,(...),0,0,0,0,0,0,0,0,0,0,0,0).
  min = 0, max = 65535, mean = 13899.9, std = 26784.6, coords_min = (0,0,0,0), coords_max = (1962,1108,0,0).

Your two trailing alpha channels appear to be identical, so their ordering as a pair seems moot. Subtracting one alpha channel from the other leaves a zero-ed out image (Note image metrics in G’MIC’s shell log).

gosgood@bertha ~/Downloads $ gmic output-alpha2.tif split. c sub[-2,-1] keep.
[gmic]-0./ Start G'MIC interpreter.
[gmic]-0./ Input all frames of TIFF file 'output-alpha2.tif' at position 0 (1 image 4096x4096x1x5).
[gmic]-1./ Split image [0] along the 'c'-axis.
[gmic]-5./ Subtract images [3,4].
[gmic]-4./ Keep image [3] (1 image left).
[gmic]-1./ Display image [0] = 'output-alpha2_c3.tif'.
[0] = 'output-alpha2_c3.tif':
  size = (4096,4096,1,1) [64 Mio of float32].
  data = (0,0,0,0,0,0,0,0,0,0,0,0,(...),0,0,0,0,0,0,0,0,0,0,0,0).
  min = 0, max = 0, mean = 0, std = 0, coords_min = (0,0,0,0), coords_max = (0,0,0,0).

Of course, with G’MIC cli you may order these channels in any permutation you desire. reverse and move are your friends. Your workflow roughly would go along the line of splitting along channels, rearranging the image list, perhaps inputting new channels, then appending back together to obtain an image with rearranged channels.

I can’t recommend another software package that affords this degree of image manipulation.

What are you hoping to achieve?

And — welcome to the board! Enjoy your stay.


I generally just want to achieve the image’s extra channels to show up as 2 additional alpha channels in photoshop. One pre-multiplied and one as a separate alpha channel. I’ve figured the way to do this is by having 2 alpha channels, I don’t however know how to tell the file how to use the channels, either “associated” or “unspecified”.

I figured that maybe if I swap the channels, the “meta data” of the channels would also be swapped and it would denote it the way I want. Trying to output my current image containing 5 channels using G’Mic seems to turn it’s color-space into “grayspace” as well however, already preventing me from getting a proper RGB(A) image.

This would be my desired result structure:
desired-output-result.tif (39.5 MB)

“Premultiplied” is not an intrinsic property of an alpha channel. The term applies to an entire image when that image’s other channels have been scaled — i.e. pre-multiplied — by the information in the alpha channel. “Premultiplied” means the data in the alpha channel has first been copied out, then the copy normalized to the range [0,…,1]. That copy is then used to multiply (scale) the other channels. Once that operation is complete, no pixel in the other channels should be numerically larger than its alpha channel counterpart, and if the counterpart in the alpha channel is zero, then so are the other channels zero. If the counterpart pixel in the alpha channel is 50% (32767 for sixteen bit channels) then the associated color data has been scaled back by 50%. This is a destructive operation for integer data channels. Once an image has been pre-multiplied by its alpha channel, the operation should not be undone.

The other alpha channel convention of note is “straight” alpha. Again, this is not an intrinsic of the alpha channel but of the image. It means that the other channel data has not in any way been multiplied by the by the alpha channel. The data in the alpha channel flags to what degree the counterpart pixels in the other channels should be regarded as transparent, but no computation has actually been done.

The notions of premultiplied or straight alpha channels are not correct. These are terms that describe an image’s relation to the provided alpha channel. Alan Melikdjanian (Captain Disillusion) has recently released a video on the distinction between “straight” and “premultiplied” alpha. He specifically calls out Photoshop as a package which misuses the term in its user interface and documentation. What it practically means is that when you set up your compositing workflow, decide up front if your image assets are going to be premultiplied or straight, then stick with whatever convention you choose for your pipeline.

Hope this distinction helps.

1 Like

I suppose it made it a bit more clear, I think I already had a decent image in my head of how the process works but watching the video put in a bit more in perspective. I suppose what Photoshop does using the 2 “straight” alpha channels is choosing to show 1 channel as if it’s pre-multiplied in the RGB channels and 1 channel to be shown as a regular “straight” alpha channel.

The notation of “(un)associated”, which I think is unique to the .tif format, telling Photoshop to treat the first alpha channel as the channel to “pre-multiply”, or I guess “composite” into the RGB Channels, and the 2nd one (unspecified) as the regular straight Alpha Channel.

When I create a .tif file in photoshop it seems to label the 2nd alpha channel to a custom name. As far as I can see I cannot write this type of tag through any other software. Would this mean that it’s impossible to replicate such an image without the use of Photoshop?

Exiftool data of .tif of the desired type of output:

@Forsaken wants two extra samples in a TIF, and wants the first of these to be “unspecified”, and the second to be “unassociated alpha”. At How to export .tiff with multiple Alpha channels. · Discussion #5840 · ImageMagick/ImageMagick · GitHub I have shown how ImageMagick can create these two channels, but IM can’t specifiy the type of the second extra channel. Nor can exiftool.

Note that neither “unspecified” or “unassociated alpha” would be pre-multiplied.

On naming the extra channels: “AlphaChannelsNames” is a tag that is specific to Photoshop. Exiftool can’t write that tag. See Photoshop Tags

1 Like

Some information here: Working with TIFF Extra Channels. There is a TIFF Directory entry called Extra Samples: which provides a count of additional channels beyond R, G, B followed by a list of terms, these delimited in angle brackets. The order entry of terms is significant and correspond to each extra channel in order of appearance in the image file. Here is an excerpt from the TIFF Directory of desired-output-result.tif as given by tiffinfo:

=== TIFF directory 0 ===
TIFF Directory at offset 0x8 (8)
  Subfile Type: (0 = 0x0)
  Image Width: 4096 Image Length: 4096
  Resolution: 72, 72 pixels/inch
  Bits/Sample: 16
  Compression Scheme: LZW
  Photometric Interpretation: RGB color
  ⇒ Extra Samples: 2<assoc-alpha, unspecified>
  Orientation: row 0 top, col 0 lhs
  Samples/Pixel: 5
  Rows/Strip: 6
  Planar Configuration: single image plane
  Software: Adobe Photoshop 22.5 (Windows)
  DateTime: 2022:12:19 14:51:25
  1. assoc-alpha: premultiplied, as I have described above. R, G and B have been scaled by a normalized version of this alpha channel.
  2. unassoc-alpha: “straight alpha” The channel conveys how transparent the R, G or B are, but does not alter the image data in any way.
  3. unspecified: The data has no associated purpose, insofar as TIFF specifications are concerned; the user who wrote the channel data may have some idea.

While gmic can manipulate and reorder channels by any permutation desired, when writing out a TIFF file, it provides no means to directly manipulate TIFF Directory information, so you cannot meaningfully associate purpose to these extra channels. It appears to me that gmic does not compose an Extra Samples: directory entry when it writes a TIFF file, so anything downstream from a gmic session would not have these channel purposes provided.

Hope this helps.

1 Like