How can I retrieve an image from a url and convert it to a PIL object

房东的猫 提交于 2019-12-11 04:36:18

问题


import urllib.request
import Image


urllib.request.urlretrieve("https://www.spriters-resource.com/download/50365/", "mario_image.PNG")
img = Image.open("mario_image.PNG")  

I'm wondering how can I retrieve an image from a url and immediately convert it to a PIL Image without having to load the image from the file


回答1:


import requests
from PIL import Image

r = requests.get(URL, stream=True)
img = Image.open(r.raw)


来源:https://stackoverflow.com/questions/41050871/how-can-i-retrieve-an-image-from-a-url-and-convert-it-to-a-pil-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!