1. 使用requests
(推荐)
1 2 3 4 |
from PIL import Image from StringIO import StringIO import requests Image.open(StringIO(requests.get(url, stream=True).raw.read())) |
2. 使用StringIO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from PIL import Image from StringIO import StringIO import requests r = requests.get("http://a/b/c") im = Image.open(StringIO(r.content)) im.size # ======================= from PIL import Image import urllib2 as urllib from StringIO import StringIO fd = urllib.urlopen("http://a/b/c") im = Image.open(StringIO(fd.read())) im.size |
3. 使用io.BytesIO
1 2 3 4 5 6 7 |
from PIL import Image import urllib2 as urllib import io fd = urllib.urlopen("http://a/b/c") image_file = io.BytesIO(fd.read()) im = Image.open(image_file) |