Masking an alpha channel with a gray scale image

I have been trying to do something with ImageMagick that seems to be a trivial task but I don't get it right.

I have a transparent PNG (32 bit) and I want to mask that one with a grayscale image (also 32 bit). The reason for the files being 32 bits is that they are generated with GIS tools that by default output 32 bits. I would rather not have to change that since all my files have already been generated.

Anyway - this is what I am using now to do this:

convert                 \
  alpha_channel.png     \
   \(                   \
      mask.png          \
       -colorspace gray \
       -alpha off       \
   \)                   \
 -compose copy-opacity  \
 -composite             \
  PNG32:output.png

The result is almost right but I think there is some problem with the bit depth. It is difficult to explain but you can easily see it by looking at the files and compare "output.png" with "expected_output.png"

Here are the files that I am working with: https://www.dropbox.com/s/855fh8svgt45mqq/images.zip?dl=0

What do you think I am doing wrong here?

Upvotes: 1

Views: 2127

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207445

It seems to me that all the information is in the alpha channel of the file alpha-channel but you are using the bi-level gray values from that file rather than the actual information which has any content.

Image: alpha_channel.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 4096x4096+0+0
  Units: Undefined
  Type: Bilevel
  Base type: Bilevel
  Endianess: Undefined
  Colorspace: Gray
  Depth: 8-bit
  Channel depth:
    gray: 1-bit
    alpha: 8-bit
  Channel statistics:
    Pixels: 16777216
    Gray:
      min: 0 (0)
      max: 255 (1)
      mean: 228.324 (0.89539)
      standard deviation: 78.0429 (0.30605)
      kurtosis: 4.67613
      skewness: -2.58382
      mean: 41.3934 (0.162327)
      standard deviation: 22.3399 (0.0876074)
      kurtosis: -0.402954
      skewness: 0.327285
  Alpha: graya(0,0)   #00000000
  Colors: 118
  Histogram:
   1755067: (  0,  0,  0,  0) #00000000 graya(0,0)
    478353: (255,255,255, 37) #FFFFFF25 graya(255,0.145098)
    378182: (255,255,255, 51) #FFFFFF33 graya(255,0.2)
    374922: (255,255,255, 47) #FFFFFF2F graya(255,0.184314)
    368496: (255,255,255, 39) #FFFFFF27 graya(255,0.152941)
    362982: (255,255,255, 49) #FFFFFF31 graya(255,0.192157)

And that you are also trying to discard the alpha channel of the file mask.png which doesn't seem right either since it has no alpha channel. So, I am confused as to where your information really is, or rather what you actually want to extract. I can only suggest the following which seems to extract what I think you may want but is nothing like what you expect. Still, it may help you get further....

convert alpha_channel.png -alpha extract \
   \( mask.png -colorspace gray \) \
   -compose copy-opacity -composite out.png

enter image description here

Upvotes: 1

Related Questions