Strange IOError when opening base64 string in PIL

a 夏天 提交于 2019-12-01 07:54:17

When you create your image_string, you're creating a fake file-like object backed by a string. When you call Image.open, it reads this fake file, moving the file pointer to the end of the file. Attempting to use Image.open on it again just gives you an EOF.

You need to either re-create your StringIO object, or seek() to the beginning of the stream.

image_string is file-like object. File-like object has file position.

Once the file is read, the position is advanced.

Any subsequent is occurred at that position unless you explicitly position it using seek method.

So if you want to reopen the file:

...
image_string.seek(0) # reset file position to the beginning of the file.
img = Image.open(image_string)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!