如何将从 url 检索到的图像放入图像小部件
How to put image retrieved from url into an image widget
我找到了这段代码,它工作正常。可以检索并显示图像。
import IPython
url = 'https://newevolutiondesigns.com/images/freebies/colorful-background-14.jpg'
IPython.display.Image(url, width = 250)
不过,我其实是想把图片放到一个图片小部件中,所以我试过这个:
import IPython
from ipywidgets import widgets
url = 'https://newevolutiondesigns.com/images/freebies/colorful-background-14.jpg'
image = IPython.display.Image(url, width = 300)
widgets.Image(
value=image,
format='jpg',
width=300,
height=400,
)
但是我得到了这个错误:
---------------------------------------------------------------------------
TraitError Traceback (most recent call last)
<ipython-input-45-5011b6084128> in <module>()
57 format='jpg',
58 width=300,
---> 59 height=400,
60 )
________________________________________
7 frames
________________________________________
/usr/local/lib/python3.6/dist-packages/traitlets/traitlets.py in error(self, obj, value)
623 e = "The '%s' trait must be %s, but a value of %r was specified." \
624 % (self.name, self.info(), repr_type(value))
--> 625 raise TraitError(e)
626
627 def get_metadata(self, key, default=None):
TraitError: The 'value' trait of an Image instance must be a bytes object, but a value of
<IPython.core.display.Image object> <class 'IPython.core.display.Image'> was specified.
所以,我的问题是:如何将图像放入图像小部件?
首先感谢您提供的最小示例。
该错误告诉您需要做什么。您需要将图像文件的 bytes
传递给 ipywidgets.Image
class。通过从您的 Ipython.display.Image
实例传递 image.data
来执行此操作。
import IPython
from ipywidgets import widgets
url = 'https://newevolutiondesigns.com/images/freebies/colorful-background-14.jpg'
image = IPython.display.Image(url, width = 300)
widgets.Image(
value=image.data,
format='jpg',
width=300,
height=400,
)
或者,使用像 requests
这样的包来获取图像的 bytes
并将它们传递给 widgets.Image
,而不需要 Ipython.display.Image
实例。
我找到了这段代码,它工作正常。可以检索并显示图像。
import IPython
url = 'https://newevolutiondesigns.com/images/freebies/colorful-background-14.jpg'
IPython.display.Image(url, width = 250)
不过,我其实是想把图片放到一个图片小部件中,所以我试过这个:
import IPython
from ipywidgets import widgets
url = 'https://newevolutiondesigns.com/images/freebies/colorful-background-14.jpg'
image = IPython.display.Image(url, width = 300)
widgets.Image(
value=image,
format='jpg',
width=300,
height=400,
)
但是我得到了这个错误:
---------------------------------------------------------------------------
TraitError Traceback (most recent call last)
<ipython-input-45-5011b6084128> in <module>()
57 format='jpg',
58 width=300,
---> 59 height=400,
60 )
________________________________________
7 frames
________________________________________
/usr/local/lib/python3.6/dist-packages/traitlets/traitlets.py in error(self, obj, value)
623 e = "The '%s' trait must be %s, but a value of %r was specified." \
624 % (self.name, self.info(), repr_type(value))
--> 625 raise TraitError(e)
626
627 def get_metadata(self, key, default=None):
TraitError: The 'value' trait of an Image instance must be a bytes object, but a value of
<IPython.core.display.Image object> <class 'IPython.core.display.Image'> was specified.
所以,我的问题是:如何将图像放入图像小部件?
首先感谢您提供的最小示例。
该错误告诉您需要做什么。您需要将图像文件的 bytes
传递给 ipywidgets.Image
class。通过从您的 Ipython.display.Image
实例传递 image.data
来执行此操作。
import IPython
from ipywidgets import widgets
url = 'https://newevolutiondesigns.com/images/freebies/colorful-background-14.jpg'
image = IPython.display.Image(url, width = 300)
widgets.Image(
value=image.data,
format='jpg',
width=300,
height=400,
)
或者,使用像 requests
这样的包来获取图像的 bytes
并将它们传递给 widgets.Image
,而不需要 Ipython.display.Image
实例。