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!
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).
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.
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.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With