PIL:来自url的图片,无法识别图片文件

时间:2013-09-01 17:28:38

标签: python http encoding gzip python-imaging-library

我正在尝试从网址访问图片:

http://www.lifeasastrawberry.com/wp-content/uploads/2013/04/IMG_1191-1024x682.jpg

但是,它在最后一步中失败并出现IOError(“无法识别图像文件”)。不确定发生了什么或如何解决它。它与许多其他网址图像一起使用。

    opener = urllib2.build_opener()
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    opener.addheaders = [('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')]
    opener.addheaders = [('Accept-Encoding', 'gzip,deflate,sdch')]

    response = opener.open(image_url,None,5)
    img_file = cStringIO.StringIO(response.read())  

    image = Image.open(img_file)

这个网址也失败了:

http://www.canadianliving.com/img/photos/biz/Greek-Yogurt-Ceaser-Salad-Dressi1365783448.jpg

1 个答案:

答案 0 :(得分:1)

问题是你告诉你的URL检索器要求服务器提供gzip编码的结果,所以你收到的图像数据是gzip编码的。您可以通过从请求中取消accept-encoding标头或手动解压缩gzip编码结果来解决此问题:

from PIL import Image
import urllib2
import gzip
import cStringIO

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
opener.addheaders = [('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')]
opener.addheaders = [('Accept-Encoding', 'gzip,deflate,sdch')]

gzipped_file = cStringIO.StringIO(opener.open(url, None, 5).read())
image = Image.open(gzip.GzipFile(fileobj=gzipped_file))

这种方法的问题在于,如果您在HTTP请求中接受多个编码,那么您需要查看结果的HTTP标头以查看您实际获得的编码,然后根据任何内容进行手动解码值表示。

我认为将accept-encoding标头设置为一个值,以便您只接受一个编码(例如,'identity;q=1, *;q=0'或类似的东西),或继续开始使用{{3}做HTTP。