Get image from image url: IOError: cannot identify image file
Get image from image url: IOError: cannot identify image file
我正在使用 Python 请求从图像 url 获取图像文件。
下面的代码在大多数情况下都有效,但在 url 秒后开始失败。
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))
如果出现错误,那么我会尝试使用不同的 header(这解决了过去的问题):
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'}
但是,这些 url(以及其他)不起作用。他们给出了 "IOError: cannot identify image file" 错误。
http://cdn.casaveneracion.com/vegetarian/2013/08/vegan-spaghetti1.jpg
http://www.rachaelray.com/site/images/sidebar-heading-more-recipes-2.svg
它使用 urls 在我的浏览器中显示图像。不知道他们有没有同样的问题。
您正在使用 Python 图像库 (PIL) 来提供代码最后一行中提到的图像 class。
- Paleo Effect 图像是一个 WebP 文件。 PIL 不支持 WebP 格式。
- Casa Veneracion URL 不会 link 图像文件 - 它 returns 302 重定向到 HTML 文件。 (See for yourself.)
- Rachael Ray 图像是一个 SVG 文件。 PIL 不支持 SVG 格式。
参见 this documentation for Image formats supported by PIL 的底部。
我正在使用 Python 请求从图像 url 获取图像文件。
下面的代码在大多数情况下都有效,但在 url 秒后开始失败。
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))
如果出现错误,那么我会尝试使用不同的 header(这解决了过去的问题):
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'}
但是,这些 url(以及其他)不起作用。他们给出了 "IOError: cannot identify image file" 错误。
http://cdn.casaveneracion.com/vegetarian/2013/08/vegan-spaghetti1.jpg
http://www.rachaelray.com/site/images/sidebar-heading-more-recipes-2.svg
它使用 urls 在我的浏览器中显示图像。不知道他们有没有同样的问题。
您正在使用 Python 图像库 (PIL) 来提供代码最后一行中提到的图像 class。
- Paleo Effect 图像是一个 WebP 文件。 PIL 不支持 WebP 格式。
- Casa Veneracion URL 不会 link 图像文件 - 它 returns 302 重定向到 HTML 文件。 (See for yourself.)
- Rachael Ray 图像是一个 SVG 文件。 PIL 不支持 SVG 格式。
参见 this documentation for Image formats supported by PIL 的底部。