Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Unknown extension' in save function of PIL due to empty EXTENSION array

Tags:

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.

like image 322
DAH Avatar asked Feb 13 '15 15:02

DAH


People also ask

How do I open a byte image in Python?

If you have an entire image in a string, wrap it in a BytesIO object, and use open() to load it.

How do I import an image into 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 save an image object in Python?

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.


1 Answers

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")
like image 93
jzs Avatar answered Sep 20 '22 10:09

jzs