当我们处理本地文件时,我试图做的事情相当简单,但当我试图用远程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()

其他回答

使用要求:

from PIL import Image
import requests
from StringIO import StringIO

response = requests.get(url)
img = Image.open(StringIO(response.content))
from PIL import Image
import cv2
import numpy as np
import requests
image=Image.open(requests.get("https://previews.123rf.com/images/darrenwhi/darrenwhi1310/darrenwhi131000024/24022179-photo-of-many-cars-with-one-a-different-color.jpg", stream=True).raw)
#image =resize((420,250))

image_array=np.array(image)
image 

在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()

自PIL >= 2.8.0以来,不再需要在BytesIO中手动包装。只需使用Image.open(response.raw)

在Vinícius的评论之上补充道:

您应该传递stream=True,如前所述https://requests.readthedocs.io/en/master/user/quickstart/#raw-response-content

So

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

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循环!