def getSize(f):
print StringIO(f)
im = Image.open(StringIO(f))
size = im.size[0], im.size[1]
return size
def download(source_url, g = False, correct_url = True):
try:
socket.setdefaulttimeout(10)
agents = ['Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)','Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1)','Microsoft Internet Explorer/4.0b1 (Windows 95)','Opera/8.00 (Windows NT 5.1; U; en)']
ree = urllib2.Request(source_url)
ree.add_header('User-Agent',random.choice(agents))
ree.add_header('Accept-encoding', 'gzip')
opener = urllib2.build_opener()
h = opener.open(ree).read()
if g:
compressedstream = StringIO(h)
gzipper = gzip.GzipFile(fileobj=compressedstream)
data = gzipper.read()
return data
else:
return h
except Exception, e:
return ""
pic = download("http://media2.ct.yelpcdn.com/photo/2MdauidaMUazuew2h0pdgQ/l")
s = getSize(pic)
当我这样做时,出现错误:
print StringIO(f)
File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 1980, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
答案 0 :(得分:3)
问题在于您的Accept-Encoding
状态gzip
因此您可能会获得一张gzip图像。
我刚刚尝试使用gzip解压缩代码,并且没有任何问题。
pic = download("http://media2.ct.yelpcdn.com/photo/2MdauidaMUazuew2h0pdgQ/l", g=True)
s = getSize(pic)
将'Accept-Encoding'
从'gzip'
更改为'image.*'
ree.add_header('User-Agent',random.choice(agents))
ree.add_header('Accept-Encoding', 'image.*')
第2部分:
你可以随时询问gzip
并使用try / except来包装,以便在gzip抱怨时不加改变地返回数据。
try:
compressedstream = StringIO(h)
gzipper = gzip.GzipFile(fileobj=compressedstream)
data = gzipper.read()
return data
except IOError: # not gzip
return h