Pillow在Python2.7.6中的Windows上持续抛出无法识别图像文件
我在32位的Windows上使用Python2.7.6和Pillow 2.3.0。而且我电脑上没有安装PIL。
我遇到的问题是,当我执行以下操作时,出现了"无法识别图像文件"的错误。
>>> from PIL import Image
>>> file = open(r"C:\\a.jpg", 'r')
>>> image = Image.open(file)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\pillow-2.3.0-py2.7-win32.egg\PIL\Image.py", line 2025, in open
IOError: cannot identify image file
但是如果我在用Image.Open
打开文件之前不先"打开"它,这个操作就能正常工作:
>>> image2 = Image.open(r"C:\\a.jpg", 'r')
注意:我不能省略"打开"这条语句。
有没有人知道这奇怪的行为可能是什么原因呢?
提前谢谢大家!
1 个回答
1
不要使用 image = Image.open(file)
,因为你已经打开了这个文件。
试试 image = Image.open("C:\\a.jpg")
。
这是图像模块的链接:http://effbot.org/imagingbook/image.htm
编辑:
打开文件时要用 'rb' 而不是 'r'。