从图像url获取图像:IOError:无法识别图像文件

时间:2015-07-14 04:53:23

标签: python python-requests

我正在使用Python请求从图片网址获取图片文件。

以下代码在大多数情况下都有效,但是越来越多的网址开始失败。

import requests
image_url = "<url_here>"
headers = {'User-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36', 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Encoding':'gzip,deflate,sdch'}
r = requests.get(image_url, headers=headers)
image = Image.open(cStringIO.StringIO(r.content))

如果这给出了错误,那么我尝试使用不同的标题(这解决了过去的问题):

headers = {'User-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36', 'Accept':'image/webp,*/*;q=0.8','Accept-Encoding':'gzip,deflate,sdch'}

但是,这些网址(以及其他网址)不起作用。它们给出了“IOError:无法识别图像文件”错误。

http://www.paleoeffect.com/wp-content/uploads/2011/06/800x414xpaleo_bread_wheat_recipe-800x414.jpg.pagespeed.ic.6pprrYPoTo.webp

http://cdn.casaveneracion.com/vegetarian/2013/08/vegan-spaghetti1.jpg

http://www.rachaelray.com/site/images/sidebar-heading-more-recipes-2.svg

使用网址在浏览器中显示图片正常。我不知道他们是否有同样的问题。

1 个答案:

答案 0 :(得分:0)

您正在使用Python Imaging Library(PIL)提供代码最后一行中提到的Image类。

  • Paleo Effect图像是一个WebP文件。 PIL不支持WebP格式。
  • Casa Veneracion网址未链接到图片文件 - 它会将302重定向返回到HTML文件。 (See for yourself。)
  • Rachael Ray图像是SVG文件。 PIL不支持SVG格式。

this documentation for Image formats supported by PIL的底部。