PIL / urllib2 - cannot identify image file when passing file using StringIO

扶醉桌前 提交于 2019-12-04 15:02:54

You need to .read() your urllib2.urlopen object:

import StringIO
from PIL import Image

image_buff = urllib2.urlopen(url).read()
image = Image.open(StringIO.StringIO(image_buff))

Try this:

from PIL import image
from StringIO import StringIO

f = urllib2.urlopen("http://www.example.com/some.jpg")
data = f.read()

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