libvips / pyvips

python binding for libvips using cffi
MIT License
642 stars 49 forks source link

composite changes number of bands #183

Open petoor opened 4 years ago

petoor commented 4 years ago

Hi John.

I have some trouble understanding the composite function. Image used in the example : img

The aim here is to have an image of size 3000 x 3000 with cat and black background.

>>> img = pyvips.Image.new_from_file("cat.jpg")
>>> black = pyvips.Image.black(3000,3000,bands=img.bands)
>>> together = black.composite(img,"over")
>>> together
<pyvips.Image 3000x3000 uchar, 4 bands, multiband>
>>> img
<pyvips.Image 1024x576 uchar, 3 bands, srgb>
>>> black
<pyvips.Image 3000x3000 uchar, 3 bands, multiband>

Why does the number of bands change? Another option would be to use draw_image, but that always assumes that the image have 1 or 3 bands which is not always the case.

Best regards

petoor commented 4 years ago

Hi again.

I found i can archive what i try to do with img.embed(0, 0, 3000, 3000) However, the question still stands, i'm not sure why composite changes the number of bands

jcupitt commented 4 years ago

Hello @petoor,

composite does fancy alpha compositing using the PDF blend modes (mix, lighten, dodge, etc.), so all the images it combines need to have alpha channels.

https://libvips.github.io/libvips/API/current/libvips-conversion.html#vips-composite

If you just want to put one solid image on top of another, try insert:

result = background.insert(foreground, 100, 100)

https://libvips.github.io/libvips/API/current/libvips-conversion.html#vips-insert

There's also join and embed (as you've found).

jcupitt commented 4 years ago

draw_image will work for any number of bands, but the two images must match, or one must have one band.

https://libvips.github.io/libvips/API/current/libvips-draw.html#vips-draw-image

But ... don't use the draw_ operations! They are just for paint programs, not for image processing. The nip2 paintbox uses them.

petoor commented 4 years ago

Thank you for the clarification :smile:

Are there any benefits (speedup / memory impact) of using insert instead of join or embed?

jcupitt commented 4 years ago

embed is the fastest, then insert, then composite.