我找到了这段代码,并且它能够正常工作,可以检索并显示图像。
但我遇到了这个错误:
那么,我的问题是:我该如何将图片放入图像小部件中?
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.
那么,我的问题是:我该如何将图片放入图像小部件中?