Pillow: alpha_composite failed

Created on 31 Jan 2016  路  4Comments  路  Source: python-pillow/Pillow

I try to merge a picture to another,but it did not work.I read the document for the function Image.alpha_composite() carefully.
there is my step and result.

In [3]: from PIL import Image
In [4]: python = Image.open("python.png")
In [5]: dota = Image.open("dota.png")
In [6]: Image.alpha_composite(dota, python).save("haha.png")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-fd0f4b6564d1> in <module>()
----> 1 Image.alpha_composite(dota, python).save("haha.png")
D:\python27\lib\site-packages\PIL\Image.pyc in alpha_composite(im1, im2)
   2311     im1.load()
   2312     im2.load()
-> 2313     return im1._new(core.alpha_composite(im1.im, im2.im))
   2314 
   2315 
ValueError: image has wrong mode
In [7]: python.size == dota.size
Out[7]: True
In [8]: python.mode == dota.mode
Out[8]: True

Is the information is enough?

Most helpful comment

I have created a PR to update the documentation.

Thanks for the images. I can tell you that if you add .convert("RGBA") to your code like so -

from PIL import Image
python = Image.open("python.png").convert("RGBA")
dota = Image.open("dota.png").convert("RGBA")
Image.alpha_composite(dota, python).save("haha.png")

Then the script will run without an error. However, given that neither of the initial images has transparency, I'm not sure what you're hoping to happen. Are you imagining that the white background of python.png is transparent, or should be treated as such?

All 4 comments

Looking at the code, the first image must be of RGBA mode, uint8 and 4 bands.

I'm guessing that dota.mode is not RGBA?

The information of mode is:

    In [3]: from PIL import Image
    In [4]: python = Image.open("python.png")
    In [5]: dota = Image.open("dota.png")
    In [6]: python.mode
    Out[6]: 'RGB'
    In [7]: dota.mode
    Out[7]: 'RGB'
    In [8]: dota.size
    Out[8]: (137, 136)
    In [9]: python.size
    Out[9]: (137, 136)

I think ,according to the document alpha_composite , two images which have the same mode and size are suitable.

I have updated the two images there.for_Pillow

I have created a PR to update the documentation.

Thanks for the images. I can tell you that if you add .convert("RGBA") to your code like so -

from PIL import Image
python = Image.open("python.png").convert("RGBA")
dota = Image.open("dota.png").convert("RGBA")
Image.alpha_composite(dota, python).save("haha.png")

Then the script will run without an error. However, given that neither of the initial images has transparency, I'm not sure what you're hoping to happen. Are you imagining that the white background of python.png is transparent, or should be treated as such?

Thanks for your help.
Meanwhile,I have found the reason according to the source of function alpha_composite in AlphaComposite.c.

Was this page helpful?
0 / 5 - 0 ratings