pyvips icon indicating copy to clipboard operation
pyvips copied to clipboard

composite changes number of bands

Open petoor opened this issue 4 years ago • 5 comments

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 avatar May 14 '20 14:05 petoor

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

petoor avatar May 14 '20 14:05 petoor

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 avatar May 14 '20 17:05 jcupitt

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.

jcupitt avatar May 14 '20 17:05 jcupitt

Thank you for the clarification :smile:

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

petoor avatar May 15 '20 07:05 petoor

embed is the fastest, then insert, then composite.

jcupitt avatar May 15 '20 08:05 jcupitt