I am rather new to python and have a problem with the save
function of the Pillow fork of PIL.
With this minimal example
import Image
im = Image.new("RGB", (200, 30), "#ddd")
im.save("image.png")
I get the following error:
File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 1667, in save
raise KeyError(ext) # unknown extension
KeyError: '.png'
The corresponding lines in the save
function are
preinit()
[...]
try:
format = EXTENSION[ext]
except KeyError:
raise KeyError(ext) # unknown extension
I looked at the EXTENSION
array and detected that it is empty, although it should be initialized in preinit()
by for example from PIL import PngImagePlugin
. PngImagePlugin.py
calls Image.register_extension("PNG", ".png")
. Watching the array inside this function or inside PngImagePlugin
it is indeed filled with file extensions.
Putting print(EXTENSION)
right before the try-except-block however shows an empty EXTENSION
array.
(Same issue with the SAVE
array a few lines down in the save
function.)
Any help is appreciated.
EDIT: I recently upgraded from OpenSuse 13.1. to 13.2. It worked fine in 13.1 but not in 13.2.
If you have an entire image in a string, wrap it in a BytesIO object, and use open() to load it.
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).
The PIL module is used for storing, processing, and displaying images in Python. To save images, we can use the PIL. save() function. This function is used to export an image to an external file.
You need to write this instead:
from PIL import Image # Notice the 'from PIL' at the start of the line
im = Image.new("RGB", (200, 30), "#ddd")
im.save("image.png")
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