PIL: pasting RGBA images to RGBA images

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

PIL: pasting RGBA images to RGBA images


Hello Everyone,

I have a problem pasting RGBA images to RGBA images.
It's a bit hard to describe the output, but it seems like the alpha channel of the original image is affected by the pasted image alpha channel, so the original images becomes transparent in places it wasn't before.
I googled around and only found answers regarding the third "mask" parameter of the paste function, but that didn't help in the case of two RGBA images.
In most PIL use cases people paste RGBA images to RGB images (ex. watermarking images) so the current setup doesn't result in any artifacts, but in my case with two RGBA images it does.

I looked further in the source code and found the related code in Paste.c:

Line 134:
for (i = 0; i < pixelsize; i++) {
        *out = BLEND(*mask, *out, *in, tmp1, tmp2);
        out++, in++;

So all 4 channels (R,G,B,A) are handled the same way.

I looked up the topic and found this useful Cairo project page which describes all of the widely used operators in image processing but none of them seemed to match this behavior. (http://cairographics.org/operators/)
Finally I modified the source code to my needs, and implemented the CAIRO_OPERATOR_OVER for PIL, handling the alpha channel blending separately, following the description in the Cairo docs.

But I am still wondering if anybody ever had the same problem, or if there is some more simple and elegant solution by just parametrize the PIL functions differently? (I couldn't find one)
Also I think the CAIRO_OPERATOR_OVER behavior is the one that most people would expect from a paste function so I would recommend to change the source code accordingly if no simple solution is available.

Thanks for your answers in advance, I hope my description is clear enough.


Image-SIG maillist  -  [hidden email]