如何从Python中的URL读取图像数据?

时间:2022-11-20 00:22:09

What I'm trying to do is fairly simple when we're dealing with a local file, but the problem comes when I try to do this with a remote URL.

当我们处理本地文件时,我要做的是相当简单的事情,但是当我尝试使用远程URL时,问题就出现了。

Basically, I'm trying to create a PIL image object from a file pulled from a URL. Sure, I could always just fetch the URL and store it in a temp file, then open it into an image object, but that feels very inefficient.

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

Here's what I have:

这就是我有:

Image.open(urlopen(url))

It flakes out complaining that seek() isn't available, so then I tried this:

它抱怨seek()不可用,因此我尝试如下:

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

But that didn't work either. Is there a Better Way to do this, or is writing to a temporary file the accepted way of doing this sort of thing?

但这也不管用。有更好的方法来做这件事吗?或者写一个临时的文件是做这种事情的公认的方法吗?

7 个解决方案

#1


153  

you could try using a StringIO

你可以尝试使用StringIO

import urllib, cStringIO

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

#2


129  

In Python3 the StringIO and cStringIO modules are gone.

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

In Python3 you should use:

在Python3中,你应该使用:

from PIL import Image
import requests
from io import BytesIO

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

#3


50  

I use the requests library. It seems to be more robust.

我使用请求库。它似乎更稳健。

from PIL import Image
import requests
from StringIO import StringIO

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

#4


27  

Use StringIO to turn the read string into a file-like object:

使用StringIO将读取的字符串转换为类似文件的对象:

from StringIO import StringIO
import urllib

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

#5


24  

For those of you who use Pillow, from version 2.8.0 you can:

对于使用枕头的用户,可以从2.8.0版本开始:

from PIL import Image
import urllib2

im = Image.open(urllib2.urlopen(url))

or if you use requests:

或者如果你使用请求:

from PIL import Image
import requests

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

References:

引用:

#6


21  

For those doing some sklearn/numpy post processing (i.e. Deep learning) you can wrap the PIL object with np.array(). This might save you from having to Google it like I did:

对于那些做一些sklearn/numpy post处理(即深度学习)的人,您可以用np.array()来包装PIL对象。这可能会让你不必像我一样参加谷歌。

from PIL import Image
import requests
import numpy as np
from StringIO import StringIO

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

#7


0  

select the image in chrome, right click on it, click on Copy image address, paste it into a str variable (my_url) to read the 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

open it;

打开它;

from PIL import Image

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

#1


153  

you could try using a StringIO

你可以尝试使用StringIO

import urllib, cStringIO

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

#2


129  

In Python3 the StringIO and cStringIO modules are gone.

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

In Python3 you should use:

在Python3中,你应该使用:

from PIL import Image
import requests
from io import BytesIO

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

#3


50  

I use the requests library. It seems to be more robust.

我使用请求库。它似乎更稳健。

from PIL import Image
import requests
from StringIO import StringIO

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

#4


27  

Use StringIO to turn the read string into a file-like object:

使用StringIO将读取的字符串转换为类似文件的对象:

from StringIO import StringIO
import urllib

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

#5


24  

For those of you who use Pillow, from version 2.8.0 you can:

对于使用枕头的用户,可以从2.8.0版本开始:

from PIL import Image
import urllib2

im = Image.open(urllib2.urlopen(url))

or if you use requests:

或者如果你使用请求:

from PIL import Image
import requests

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

References:

引用:

#6


21  

For those doing some sklearn/numpy post processing (i.e. Deep learning) you can wrap the PIL object with np.array(). This might save you from having to Google it like I did:

对于那些做一些sklearn/numpy post处理(即深度学习)的人,您可以用np.array()来包装PIL对象。这可能会让你不必像我一样参加谷歌。

from PIL import Image
import requests
import numpy as np
from StringIO import StringIO

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

#7


0  

select the image in chrome, right click on it, click on Copy image address, paste it into a str variable (my_url) to read the 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

open it;

打开它;

from PIL import Image

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