Negative cropping / padding
Closed this issue · 4 comments
I haven't found a simple way to add spacing around an image. Cropping seems to want positive integers. Thumbnail doesn't do it (in my attempts it produces a smaller image than requested).
I ended up doing it this way:
my_img
|> Image.from_binary()
|> elem(1)
|> Vix.Vips.Operation.extract_band!(3)
Image.new!(120, 120)
|> Image.Draw.image(img, 10, 10)
|> elem(1)
|> Image.write!(:memory, suffix: ".jpg", quality: 100)
A few things along the way:
- from_binary! would be fantastic to have, have run into that a lot recently :)
- Image.Draw.image! would also be swell of course
- Drawing an image with an alpha band on top of another image, utilizing the alpha levels, seems like it should have a fairly obvious concept to it. I haven't looked into how libvips would suggest to do it but I bet it is in there.
- I think I get lucky that I got black on black after removing the alpha :) Likely not guaranteed.
And of course, I'd have done none of this if there was a negative crop, padding or margin operation :)
Still, it got the job done so much appreciated.
I haven't found a simple way to add spacing around an image
This is done by Vix.Vips.Operation.embed/6
but I haven't put an Image
API for that since I wasn't sure what the most expressive way is to do that. What do you think it should be called? Image.embed
? Image.pad
? Image.margin
? Very open to suggestions.
The current approach I would take is:
iex> background = Image.new!(width: Image.width(base_image) + margin * 2, height: Image.height(base_image) + margin * 2, color: background_color)
iex> Image.compose(base_image, background, x: :center, y: :middle)
from_binary! would be fantastic to have, have run into that a lot recently :)
Will definitely do this week.
Image.Draw.image! would also be swell of course
Image mutuation should be a "last resort" since it goes against the overall principles of libvips. I hope that Image.compose
fulfils this requirement.
Drawing an image with an alpha band on top of another image
Does Image.compose
do what you're after? That's basically what it does.
This example demonstrates what I'm referring to: https://elixirforum.com/t/image-an-image-processing-library-based-upon-vix/47568/17
Image.compose looks like it should solve all my problems. I just didn't find it :)