Python,IOError:无法识别图像文件

Python,IOError:无法识别图像文件,python,python-2.7,image-processing,python-imaging-library,Python,Python 2.7,Image Processing,Python Imaging Library,我知道有很多类似的问题,但我认为,没有鬣蜥(我认为)。我有一个图像的URL数组,我想下载它。但是,当我试图保存图像时,我得到了这个错误。我不知道如何使它工作 这是我的代码: listOfImagesUrl = ['https://cdn.psychologytoday.com/sites/default/files/blogs/1023/2012/09/105928-103553.jpg', 'http://i.livescience.com/images/i/000/048/264/origi

我知道有很多类似的问题,但我认为,没有鬣蜥(我认为)。我有一个图像的URL数组,我想下载它。但是,当我试图保存图像时,我得到了这个错误。我不知道如何使它工作

这是我的代码:

listOfImagesUrl = ['https://cdn.psychologytoday.com/sites/default/files/blogs/1023/2012/09/105928-103553.jpg', 'http://i.livescience.com/images/i/000/048/264/original/disgusted-101130-02.jpg%3F1324346664', 'http://barfblog.com/wp-content/uploads/images/disgust.story.jpg', 'http://cache1.asset-cache.net/gc/148190074-people-making-disgusted-faces-gettyimages.jpg%3Fv%3D1%26c%3DIWSAsset%26k%3D2%26d%3Dww%252BvNwEe%252BXzLnQze1Z2w9KNDivKR%252BEqGJ2cPfDe1oeinIezLX%252B8y1tIG3LNjTbL5']

imageNumber = 1

for imageUrl in listOfImagesUrl:

    file = cStringIO.StringIO(urllib.urlopen(imageUrl).read())
    img = Image.open(file)
    img.save("/tmp/test/" + str(imageNumber) + "." + img.format)
    print "DONE: " + str(imageNumber) + " of " + str(len(listOfImagesUrl))
    imageNumber += 1

我用Sleeplessner对stackoverflow问题的回答解决了url问题。问题是我必须在urllib2上启用Cookie。

我切换到
urllib2
,并如图所示重新构造代码以提供更多错误信息。看起来你的大多数图片URL都不好

from urllib2 import urlopen, URLError
from cStringIO import StringIO
from PIL import Image

listOfImagesUrl = [
    'http://barfblog.com/wp-content/uploads/images/disgust.story.jpg',
    'https://cdn.psychologytoday.com/sites/default/files/blogs/1023/2012/09/105928-103553.jpg',
    'http://i.livescience.com/images/i/000/048/264/original/disgusted-101130-02.jpg%3F1324346664',
    'http://cache1.asset-cache.net/gc/148190074-people-making-disgusted-faces-gettyimages.jpg%3Fv%3D1%26c%3DIWSAsset%26k%3D2%26d%3Dww%252BvNwEe%252BXzLnQze1Z2w9KNDivKR%252BEqGJ2cPfDe1oeinIezLX%252B8y1tIG3LNjTbL5'
]

for imageNumber, imageUrl in enumerate(listOfImagesUrl, start=1):
    try:
        url = urlopen(imageUrl)
    except URLError as e:
        print "skipping {}".format(imageUrl)
        print "  error: {}".format(e)
        continue
    file = StringIO(url.read())
    img = Image.open(file)
    img.save("/tmp/test/" + str(imageNumber) + "." + img.format)
    print "DONE: " + str(imageNumber) + " of " + str(len(listOfImagesUrl))

当加载
urllib
且响应正文为空时,第二个URL会给您一个500错误。在尝试读取数据之前,请检查您的回答。
%3F
序列是一个编码的问号。它不应该是URL路径的一部分,而是在URL中编码了一个查询参数(可能是cache buster)。最后一个URL是相同的。但是,如果我重新编码第二个URL,它仍然不工作。发生了什么事?感谢您的帮助。结尾说“自版本2.6以来已弃用:
urlopen()
函数已在Python 3中被删除,以支持”。谢谢,我更改为urllib2,并收到一个新错误。urllib2.HTTPError:HTTP错误301:HTTP服务器返回一个重定向错误,该错误将导致无限循环。最后一条30倍的错误消息是:永久移动。我现在将搜索此错误的修复程序。谢谢你,martineau,你的代码比我的好得多。有一件事我不明白,为什么我会犯这些错误?如果我在浏览器上打开URL,它会显示一个图像。发生了什么事?@Caaarlos:谢谢你接受我的回答,尽管我不知道需要启用cookie才能让URL正常工作。