我尝试在 python shell 中对图像进行编码和解码。我第一次在 PIL 中打开解码的 base64 字符串时没有错误,如果我重复 Image.open() 命令我得到IOError: cannot identify image file。
>>> with open("/home/home/Desktop/gatherify/1.jpg", "rb") as image_file:
... encoded_string = base64.b64encode(image_file.read())
>>> image_string = cStringIO.StringIO(base64.b64decode(encoded_string))
>>> img = Image.open(image_string)
>>> img
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=576x353 at 0xA21F20C>
>>> img.show() <-- Image opened without any errors.
>>> img = Image.open(image_string)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 1980, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
为什么会这样?