elixir-image / image

Image processing for Elixir

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

warp_perspective doesn't work on PNG or images with transparency

kwando opened this issue · comments

Looks like it is PNGs and images with transparency that is the problem :) I tried to convert my example image to a jpg first and then it worked.

streetview
streetview

https://elixirforum.com/t/image-an-image-processing-library-based-upon-vix/47568/106?u=kwando

Sorry, for the noise in your forum thread. I'll post my findings here in the future :)

Mix.install([
  {:image, "~> 0.28"},
  :nx,
  :evision,
  :kino
])

source_img = Image.open!("streetview.png")

image = source_img
width = Image.width(image)
height = Image.height(image)

{width, height}

Image.warp_perspective!(
  image,
  [{139, 125}, {826, 74}, {796, 559}, {155, 483}],
  [{139, 125}, {815, 125}, {815, 528}, {139, 528}]
)

Would be nice if the result image could have an alpha channel / transparent background as well :)

All good on the forum post - just better to resolve issue here.

I'm surprised that an image with alpha fails because I deliberately flatten the source image before warping so I need to work out why that's not working.

Definitely would like to warp the alpha band too, just not confident I have the knowledge.

After midnight here and I have a very early start so I I'm to have a resolution ina few hours.

Thanks for always being willing to try this stuff out!

I was thinking something like this:

Image.warp_perspective!(
  image,
  [{139, 125}, {826, 74}, {796, 559}, {155, 483}],
  [{139, 125}, {815, 125}, {815, 528}, {139, 528}],
  [background: :transparent]
)

So when the warped images becomes smaller all the added pixels will be transparent. I got it working but had to do 2 warp_perspective! + add_alpha! to get the result I was looking for. (Worked around the add_alpha-bug, see PR).

Skärmavbild 2023-04-06 kl  17 59 11

No hurry, I'm just toying around anyways :)

Thanks for the PR (accepted) and I've fixed the issue of warping an image with alpha. The image will be flattened first since I don't know how to warp including the alpha band. However this is a "trick" to make the added pixels transparent using Image.chroma_key/2.

iex> chroma_green = [1, 177, 64]
iex> "./test/support/images/image_with_alpha2.png"
...> |> Image.open!()
...> |> Image.warp_perspective!(
...>   [{139, 125}, {826, 74}, {796, 559}, {155, 483}],
...>   [{139, 125}, {815, 125}, {815, 528}, {139, 528}],
...>   background: chroma_green
...> )
...> |> Image.chroma_key!(color: chroma_green, threshold: 0)

I'd love someone to work out how to warp a 4-band image and a PR would of course be welcome!

I published Image version 0.28.1 with the following changelog entry:

Bug Fixes

  • Fixes Image.add_alpha/2. Thanks to @kwando for the PR. Closes #71.

  • Fixes :threshold option in Image.Options.ChromaKey to allow a value of 0.

  • Fixes Image.warp_perspective/4 to correctly flatten an image with an alpha band before the transform. Closes #70.

Thanks much for the PR, collaboration and motivation!