1. 使用 urllib2
#!/usr/bin/env python
#encoding=utf-8
import urllib2
import cStringIO
from PIL import Image
url = 'http://www.01happy.com/wp-content/uploads/2012/09/bg.png'
file = urllib2.urlopen(url)
tmpIm = cStringIO.StringIO(file.read())
im = Image.open(tmpIm)
print im.format, im.size, im.mode
2. 使用 requests
和上面的方法大同小异,只不过网络请求部分使用了 requests 库。
#!/usr/bin/env python
#encoding=utf-8
import requests
import cStringIO
from PIL import Image
url = 'http://www.01happy.com/wp-content/uploads/2012/09/bg.png'
response = requests.get(url)
tmpIm = cStringIO.StringIO(response.content)
im = Image.open(tmpIm)
print im.format, im.size, im.mode
[参考文献]
网友评论