问题:如何从Python中的URL读取图像数据?
当我们处理本地文件时,我想做的事情很简单,但是当我尝试使用远程URL时,问题就来了。
基本上,我试图从URL提取的文件中创建一个PIL图像对象。当然,我总是可以仅获取URL并将其存储在临时文件中,然后将其打开到图像对象中,但这感觉效率很低。
这是我所拥有的:
Image.open(urlopen(url))
它抱怨seek()
说不可用,所以我尝试了这个:
Image.open(urlopen(url).read())
但这也不起作用。有没有更好的方法可以执行此操作,还是可以将这种方式写入临时文件?
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.
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.
Here’s what I have:
Image.open(urlopen(url))
It flakes out complaining that seek()
isn’t available, so then I tried this:
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?
回答 0
在Python3中,StringIO和cStringIO模块不见了。
在Python3中,您应该使用:
from PIL import Image
import requests
from io import BytesIO
response = requests.get(url)
img = Image.open(BytesIO(response.content))
In Python3 the StringIO and cStringIO modules are gone.
In Python3 you should use:
from PIL import Image
import requests
from io import BytesIO
response = requests.get(url)
img = Image.open(BytesIO(response.content))
回答 1
你可以尝试使用StringIO
import urllib, cStringIO
file = cStringIO.StringIO(urllib.urlopen(URL).read())
img = Image.open(file)
you could try using a StringIO
import urllib, cStringIO
file = cStringIO.StringIO(urllib.urlopen(URL).read())
img = Image.open(file)
回答 2
我使用请求库。它似乎更强大。
from PIL import Image
import requests
from StringIO import StringIO
response = requests.get(url)
img = Image.open(StringIO(response.content))
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))
回答 3
对于使用Pillow的用户,从2.8.0版开始,您可以:
from PIL import Image
import urllib2
im = Image.open(urllib2.urlopen(url))
或者,如果您使用requests
:
from PIL import Image
import requests
im = Image.open(requests.get(url, stream=True).raw)
参考文献:
For those of you who use Pillow, from version 2.8.0 you can:
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:
回答 4
使用StringIO
转读字符串转换为一个类文件对象:
from StringIO import StringIO
import urllib
Image.open(StringIO(urllib.requests.urlopen(url).read()))
Use StringIO
to turn the read string into a file-like object:
from StringIO import StringIO
import urllib
Image.open(StringIO(urllib.requests.urlopen(url).read()))
回答 5
对于进行某些sklearn / numpy后处理(即深度学习)的用户,可以使用np.array()包装PIL对象。这样可以避免您像我一样去过Google:
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)))
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:
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)))
回答 6
Python 3
from urllib.request import urlopen
from PIL import Image
img = Image.open(urlopen(url))
img
Jupyter Notebook和IPython
import IPython
url = 'https://newevolutiondesigns.com/images/freebies/colorful-background-14.jpg'
IPython.display.Image(url, width = 250)
与其他方法不同,此方法还可以在for循环中使用!
Python 3
from urllib.request import urlopen
from PIL import Image
img = Image.open(urlopen(url))
img
Jupyter Notebook and IPython
import IPython
url = 'https://newevolutiondesigns.com/images/freebies/colorful-background-14.jpg'
IPython.display.Image(url, width = 250)
Unlike other methods, this method also works in a for loop!
回答 7
如今,可以建议使用的图像输入/输出方法是使用专用的软件包ImageIO。可以使用以下简单代码行直接从URL读取图像数据:
from imageio import imread
image = imread('https://cdn.sstatic.net/Sites/stackoverflow/img/logo.png')
此页面上的许多答案早于该软件包的发布,因此没有提及。ImageIO最初是Scikit-Image工具箱的组件。除了流行的图像处理库PILlow提供的格式外,它还支持多种科学格式。它将所有内容包装在仅关注图像输入/输出的干净API中。实际上,SciPy 取消了自己的图像读取器/写入器,转而使用ImageIO。
The arguably recommended way to do image input/output these days is to use the dedicated package ImageIO. Image data can be read directly from a URL with one simple line of code:
from imageio import imread
image = imread('https://cdn.sstatic.net/Sites/stackoverflow/img/logo.png')
Many answers on this page predate the release of that package and therefore do not mention it. ImageIO started out as component of the Scikit-Image toolkit. It supports a number of scientific formats on top of the ones provided by the popular image-processing library PILlow. It wraps it all in a clean API solely focused on image input/output. In fact, SciPy removed its own image reader/writer in favor of ImageIO.
回答 8
选择chrome图像,右键单击它,单击Copy image address
,将其粘贴到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()
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:
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()