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

て烟熏妆下的殇ゞ 提交于 2019-12-23 06:49:16

问题


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.


回答1:


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")


来源:https://stackoverflow.com/questions/28503419/unknown-extension-in-save-function-of-pil-due-to-empty-extension-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!