首页 > 代码库 > python直接下载图片到内存

python直接下载图片到内存

1. 使用requests(推荐)

from PIL import Image
import requests
Image.open(requests.get(url, stream=True).raw)

 

2. 使用StringIO

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

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)

 

python直接下载图片到内存