在Python中显示/获取URL中的图像

时间:2018-03-04 05:44:05

标签: python

我是python的新手。但我得到了一个任务,我需要从URL显示/获取图像。 我一直在使用带有python的Jupyter笔记本来尝试这样做。

import sys
print(sys.version)
3.5.2 |Anaconda 4.1.1 (64-bit)| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]

我试图像在this post中那样做,但没有一个答案有用。

使用

import urllib, cStringIO

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

我明白了:

ImportError                               Traceback (most recent call last)
<ipython-input-33-da63c9426dad> in <module>()
      1 url='http://images.mid-day.com/images/2017/feb/15-Justin-Bieber.jpg'
      2 print(url)
----> 3 import urllib, cStringIO
      4 
      5 file = cStringIO.StringIO(urllib.urlopen(URL).read())

ImportError: No module named 'cStringIO'

使用:

from PIL import Image
import requests
from io import BytesIO

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

我明白了:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-168cd6221ea3> in <module>()
      1 #response = requests.get("https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg")
----> 2 response.read("https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg").decode('utf-8')
      3 img = Image.open(StringIO(response.content))

AttributeError: 'Response' object has no attribute 'read'

使用:

from PIL import Image
import requests
from StringIO import StringIO

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

我明白了:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-37-5716207ad35f> in <module>()
      3 from PIL import Image
      4 import requests
----> 5 from StringIO import StringIO
      6 
      7 response = requests.get(url)

ImportError: No module named 'StringIO'

等...

我认为这将是一项轻松的任务,但到目前为止,我还没有找到答案。 我真的希望有人可以帮助我

3 个答案:

答案 0 :(得分:1)

这对我有用

from PIL import Image
import requests
from io import BytesIO

url = "https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg"
response = requests.get(url)
img = Image.open(BytesIO(response.content))
img.show()

您收到错误是因为您使用了行response.read("https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg").decode('utf-8')。我将切换回使用response = requests.get(url)

此外,对于您的错误:ImportError: No module named 'cStringIO',您使用的是python3。来自python 2的StringIO和cStringIO在python 3中被删除。请改用from io import StringIO。有关详细信息,请参阅StringIO in Python3

答案 1 :(得分:0)

这可能与https://stackoverflow.com/a/46954931/4010864重复。

对于PIL的第三个选项,您可以尝试:

from PIL import Image
import requests
import matplotlib.pyplot as plt

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

plt.imshow(img)
plt.show()

答案 2 :(得分:-1)

这是一种在python中显示来自URL的图像的简单方法!

首次安装:tkhtmlview

然后代码:

from tkinter import *
from tkhtmlview import HTMLLabel
root=Tk()
root.title("Image in python")
img=HTMLLabel(html="<img src='https://th.bing.com/th/id/OIP.VHhdPchdeDyXv50LwJ7bVgHaFO'></img>")
img.grid()
root.mainloop()