Friday, 15 August 2014

python - Opening PNG with PIL/Pillow -


I try to use PIL / pillow in Python Open a PNG image you thought it would be trivial, but the images are being corrupted.

Here is an example image:

I loaded it and showed it, tried to use Python 3.4 and Pillow 2.7.0:

  $ python dragon 3.4.0 (v3.4.0: 04f714765c13, 16) More information for Win32 type "Help", "Copyright", "Credit" or "License" on [March 2014, 19:25:23] [MSc v.1600 64 bit (AMD 64)] & Gt; & Gt; & Gt; Import PIL.Image & gt; & Gt; & Gt; Image = PIL.Image.open (r'C: \ Users \ Administrator \ Dropbox \ Desktop \ example.png ') & gt; & Gt; & Gt; Image.show ()> gt; & Gt; & Gt;  

What I display is this:

Why does anyone have any ideas and how to solve it? (Corruption happens not only when I show it, but whenever I try to paste it in another image, which is my basic requirement.)

As shown, the image is being converted to RGB. Unfortunately this means that the alpha channel is only being released. You want to make your own conversion that mixes the image with a white background.

  Image.composite (image, image new ('rgb', image size, 'white'), image). The documentation for Show ()  

Paste indicates that it also ignores the alpha channel, you can image the image in two places, one for the source and Must specify for a mask.

  base.paste (image, box, image)  

No comments:

Post a Comment