Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use Image.putalpha() on a png file from PIL lib. OSError: cannot write mode PA as PNG

Decided to try PIL lib in jupyter notebook. I have an image of blue color (nothing else) in png format.

Wanted to make it half-transparent. So I did:

from PIL import Image
blue = Image.open("blue_color.png")

When I open the image through jupyter, everything is fine. But then I apply .putalpha() method:

blue.putalpha(128)

And get this:

KeyError                                  Traceback (most recent call last)
~\Anaconda3\lib\site-packages\PIL\PngImagePlugin.py in _save(im, fp, filename, chunk)
    799     try:
--> 800         rawmode, mode = _OUTMODES[mode]
    801     except KeyError:

KeyError: 'PA'

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
~\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

~\Anaconda3\lib\site-packages\PIL\Image.py in _repr_png_(self)
    698         """
    699         b = io.BytesIO()
--> 700         self.save(b, "PNG")
    701         return b.getvalue()
    702 

~\Anaconda3\lib\site-packages\PIL\Image.py in save(self, fp, format, **params)
   2082 
   2083         try:
-> 2084             save_handler(self, fp, filename)
   2085         finally:
   2086             # do what we can to clean up

~\Anaconda3\lib\site-packages\PIL\PngImagePlugin.py in _save(im, fp, filename, chunk)
    800         rawmode, mode = _OUTMODES[mode]
    801     except KeyError:
--> 802         raise IOError("cannot write mode %s as PNG" % mode)
    803 
    804     #

OSError: cannot write mode PA as PNG

Made the same actions with another file of color, but it was in jpg format. And everything was fine!

Is it a file format problem? Could anyone tell me how to fix this?

Thanks in advance!

like image 845
Yoda_babe Avatar asked Jul 07 '20 16:07

Yoda_babe


People also ask

How do I import an image into Python using PIL?

To load the image, we simply import the image module from the pillow and call the Image. open(), passing the image filename. Instead of calling the Pillow module, we will call the PIL module as to make it backward compatible with an older module called Python Imaging Library (PIL).

How do I display an image in PIL?

Python – Display Image using PIL To show or display an image in Python Pillow, you can use show() method on an image object. The show() method writes the image to a temporary file and then triggers the default program to display that image. Once the program execution is completed, the temporary file will be deleted.

What is PIL image format?

Python Imaging Library is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and saving many different image file formats. It is available for Windows, Mac OS X and Linux. The latest version of PIL is 1.1.

What is image Fromarray?

fromarray - Function. 1.1.1 Construct a CASA image from a numerical (integer or float) array. This function converts a numerical (integer or float) numpy array of any size and dimensionality into a CASA image. It will create both float and complex valued images.


1 Answers

You can convert the png to jpg first.

im = Image.open("blue_color.png")
rgb_im = im.convert('RGB')
rgb_im.save('blue_color.jpg')
like image 55
fcmdhyd Avatar answered Oct 04 '22 18:10

fcmdhyd