[Image-SIG] Help with PIL, Imagemagick Composite function in PIL?

Alexey Borzenkov snaury at gmail.com
Tue Apr 10 11:07:21 CEST 2007


On 4/10/07, César Pérez <kzn.kpolice at gmail.com> wrote:
> Hi,
> I am new to this list but and i have a small problem with PIL.
>
> I am looking for a function that works like composite does in
> imagemagick.

[..snip..]

> from PIL import Image
>
> dtop = Image.open("dtop.png")
> frame = Image.open("frame.png")
>
> dtop.paste(frame,(0,0),frame)
> dtop.save("test.png")
> ------------------------
> I tried every form of paste but I always get this result or worst.

The problem you have happens because alpha channel of images *also*
gets composited using the mask you specified. To do it right you
actually need to split image, save target image alpha channel and
after compositing merge it back using original alpha channel:

from PIL import Image

dtop = Image.open("dtop.png")
frame = Image.open("frame.png")

assert dtop.mode == "RGBA"
r,g,b,a = dtop.split()
dtop = Image.merge("RGB", (r,g,b))
dtop.paste(frame,(0,0),frame)
r,g,b = dtop.split()
dtop = Image.merge("RGBA", (r,g,b,a))
dtop.save("test.png")

Best regards,
Alexey.


More information about the Image-SIG mailing list