List Info

Thread: Help with PIL, Imagemagick Composite function in PIL?




Help with PIL, Imagemagick Composite function in PIL?
country flaguser name
Mexico
2007-04-10 01:34:11
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. If you don't know, composite puts the first
image above the
second one and it's like pasting.

I have tried PIL paste but I can't get it to work as I want,
specially
if my images have alpha channels. 

For example I have an example script:
------------------------
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.

Using the images from [1] the output image is different, I
uploaded the
output file created by IM and PIL to [1] and as you can see
it's not the
same. Imagemagick output is im.png and PIL is pil.png. 

You can clearly see the difference if you put the images
against a
background color other than white or by opening in something
like GIMP
or any viewer that shows a checker pattern to represent the
transparency.

Imagemagick does it as I want but PIL doesn't.

I hope somebody can help or knows how to do it because I
prefer using
only PIL as it's the only thing that I don't know how to do
without
Imagemagick.


1. http://www.ca
pc-online.net/images/temp/




THX

César Pérez







_______________________________________________
Image-SIG maillist  -  Image-SIGpython.org
htt
p://mail.python.org/mailman/listinfo/image-sig
Re: Help with PIL, Imagemagick Composite function in PIL?
user name
2007-04-10 04:07:21
On 4/10/07, César Pérez <kzn.kpolicegmail.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.
_______________________________________________
Image-SIG maillist  -  Image-SIGpython.org
htt
p://mail.python.org/mailman/listinfo/image-sig
Re: Help with PIL, Imagemagick Composite function in PIL?
country flaguser name
New Zealand
2007-04-10 18:48:53
Alexey Borzenkov wrote:

> 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:

Alexey,  I think you are completely right about the problem,
but the
solution can be quite a bit simpler:

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

dtop.paste(frame.convert('RGB'), (0,0), frame)
dtop.save("test.png")


I would say that this alpha-merging quirk comes close to
being a bug --
it's probably not what most people expect.

douglas

_______________________________________________
Image-SIG maillist  -  Image-SIGpython.org
htt
p://mail.python.org/mailman/listinfo/image-sig

Re: Help with PIL, Imagemagick Composite function in PIL?
country flaguser name
Mexico
2007-04-10 23:00:58
Thanks to both of you and yes I was expecting the paste
function to work
just like a paste in any Image Editor.

César

On Wed, 2007-04-11 at 11:48 +1200, Douglas Bagnall wrote:
> Alexey Borzenkov wrote:
> 
> > 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:
> 
> Alexey,  I think you are completely right about the
problem, but the
> solution can be quite a bit simpler:
> 
> dtop = Image.open("dtop.png")
> frame = Image.open("frame.png")
> 
> dtop.paste(frame.convert('RGB'), (0,0), frame)
> dtop.save("test.png")
> 
> 
> I would say that this alpha-merging quirk comes close
to being a bug --
> it's probably not what most people expect.
> 
> douglas
> 

_______________________________________________
Image-SIG maillist  -  Image-SIGpython.org
htt
p://mail.python.org/mailman/listinfo/image-sig
Re: Help with PIL, Imagemagick Composite function in PIL?
user name
2007-04-11 00:26:45
On 4/11/07, Douglas Bagnall <douglasparadise.net.nz> wrote:
> Alexey,  I think you are completely right about the
problem, but the
> solution can be quite a bit simpler:
>
> dtop = Image.open("dtop.png")
> frame = Image.open("frame.png")
>
> dtop.paste(frame.convert('RGB'), (0,0), frame)
> dtop.save("test.png")

Hmm... The good part I see in this solution is that when
image is
converted from RGBA to RGB it just so happens that its
pixelsize is
still 4 but 4th byte is filled with 255. Then
paste_mask_RGBA kicks
in, and if dtop has alpha channel it will composite its
alpha channel
with 255, decreasing dtop transparency where frame is not
completely
transparent. To be honest I'm not sure what is generally
expected in
this situation, though looking at the way antigrain does
rgba blending
I see that it essentially has this formula:

    a = a * (1 - alpha) + 1 * alpha

Which is just the way it works in the code above (and
absolutely not
the way it'd worked in my code). So if this behavior is
expected my
example wouldn't even work as it should, thanks for showing
that. 

Best regards,
Alexey.
_______________________________________________
Image-SIG maillist  -  Image-SIGpython.org
htt
p://mail.python.org/mailman/listinfo/image-sig

[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )