elixir-image/image

`Image.embed` does mysterious things with transparent images

Closed this issue · 8 comments

Source image: https://hexdocs.pm/hexdocs-text.png

{width, height, _bands} = Image.shape(source_image)
img = Image.embed(source_image, width, height + 50, x: 0, y: 0, extend_mode: :black)

Result:

image

Basically I think it says "black" for an RGBA image is [0, 0, 0, 0], not [0, 0, 0, 255]. extend_mode: :white appears to work properly, as does extend_mode: :background, background: [0, 0, 0, 255]. However, background: :black and other such options produce errors. Might be an upstream issue, I had similar issues with Vix.Vips.Operation.embed and composite2 but I don't know enough about libvips to really guess what's going on.

Version: image 0.37.0

Addendum: in the case of a 1-channel greyscale image, background: [0] and background: 0 cause runtime errors but background: :black works fine.

Sorry for taking a while to get to this. I can see the issue you raise and fixing it is straight forward - just need to agree with the expected intent should be.

  1. extend_mode: :black does, as you suggest, get interpreted as [0,0,0,0] which results in a transparent extension on an image with an alpha channel. I think you are suggesting the default should be [0,0,0,255] meaning opaque black. I think I'm just marginally in favour of [0,0,0,255]. Perhaps an optional :background_transparency option that defaults to 255? WDYT?

  2. I will fix the extend_mode: :background, background: <color> to use an RGBA color when the image has an alpha band.

  3. For a one-channel image I can also fix that. I suppose in that case the only value colors are greyscale so I need to filter on CSS colors that are greyscale and allow only integer, or list of a single integer, values.

I will have this done and a new release out over the weekend.

I think I have this fixed now. Would you consider giving it a test, configuring from Github? ie {:image, github: "kipcole9/image"}.

I've added a :background_transparency that is used with either extend_mode: :black, extend_mode: :white or when the image has an alpha band.

I'll give it a try, thanks. Sorry in return for not getting back to you sooner. I just tracked down a Dialyzer complaint in my program to Image.embed's :extend_mode not matching what it expects, so this is on my radar again.

Please do let me know what the dialyzer error is.

I believe I've fixed the dialyzer error.

I've published image version 0.38.0 and will close this issue for now. Please re-open if the release doesn't work as expected for you.

Ayyyyy the dialyzer error is indeed fixed. The weird band-number inconsistencies also appear fixed. Thank you!