当我们处理本地文件时,我试图做的事情相当简单,但当我试图用远程URL这样做时,问题就来了。

基本上,我试图从一个URL提取的文件创建一个PIL图像对象。当然,我总是可以只获取URL并将其存储在临时文件中,然后将其打开到一个图像对象中,但这感觉非常低效。

以下是我所拥有的:

Image.open(urlopen(url))

它抱怨seek()不可用,所以我尝试了这个:

Image.open(urlopen(url).read())

但这也不管用。是否有更好的方法来做到这一点,或者写入临时文件是做这类事情的公认方法?


当前回答

在chrome中选择图像,右键单击它,单击复制图像地址,将其粘贴到str变量(my_url)来读取图像:

import shutil
import requests

my_url = 'https://www.washingtonian.com/wp-content/uploads/2017/06/6-30-17-goat-yoga-congressional-cemetery-1-994x559.jpg'
response = requests.get(my_url, stream=True)
with open('my_image.png', 'wb') as file:
    shutil.copyfileobj(response.raw, file)
del response

打开它;

from PIL import Image

img = Image.open('my_image.png')
img.show()

其他回答

在Python3中,StringIO和cStringIO模块消失了。

在Python3中,您应该使用:

from PIL import Image
import requests
from io import BytesIO

response = requests.get(url)
img = Image.open(BytesIO(response.content))

使用要求:

from PIL import Image
import requests
from StringIO import StringIO

response = requests.get(url)
img = Image.open(StringIO(response.content))

Python 3

from urllib.request import urlopen
from PIL import Image

img = Image.open(urlopen(url))
img

木星笔记本和ipyon

import IPython
url = 'https://newevolutiondesigns.com/images/freebies/colorful-background-14.jpg'
IPython.display.Image(url, width = 250)

与其他方法不同,此方法也适用于for循环!

以下代码适用于Python 3:

from PIL import Image
import requests

im = Image.open(requests.get(url, stream=True).raw)

引用:

https://github.com/python-pillow/Pillow/pull/1151 https://github.com/python-pillow/Pillow/blob/master/CHANGES.rst#280-2015-04-01

使用StringIO

import urllib, cStringIO

file = cStringIO.StringIO(urllib.urlopen(URL).read())
img = Image.open(file)