'Unknown extension' in save function of PI

2019-06-15 00:42发布

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条回答
Fickle 薄情
2楼-- · 2019-06-15 00:59

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")
查看更多
登录 后发表回答