Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将从url检索到的图像放入图像小部件_Python_Image_Widget_Ipywidgets - Fatal编程技术网

Python 如何将从url检索到的图像放入图像小部件

Python 如何将从url检索到的图像放入图像小部件,python,image,widget,ipywidgets,Python,Image,Widget,Ipywidgets,我找到了这个代码,它工作得很好。可以检索和显示图像 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 =

我找到了这个代码,它工作得很好。可以检索和显示图像

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.
---------------------------------------------------------------------------
追踪恐怖追踪(最近一次呼叫最后一次)
在()
57 format='jpg',
58宽度=300,
--->59高度=400,
60 )
________________________________________
7帧
________________________________________
/usr/local/lib/python3.6/dist-packages/traitlets/traitlets.py出错(self、obj、value)
623 e=““%s”特征必须是%s,但指定了值%r。”\
624%(self.name,self.info(),报告类型(值))
-->625(e)
626
627 def get_元数据(自身、密钥、默认值=无):
TraitError:映像实例的“value”特征必须是bytes对象,但值为
已指定。

因此,我的问题是:如何将图像放入图像小部件中?

首先感谢您提供的示例

错误会告诉您需要做什么。您需要将映像文件的
字节
传递给
ipywidgets.image
类。通过从
Ipython.display.image
实例传递
image.data
来完成此操作

导入IPython
从ipywidgets导入小部件
url='1〕https://newevolutiondesigns.com/images/freebies/colorful-background-14.jpg'
image=IPython.display.image(url,宽度=300)
widgets.Image(
值=image.data,
format='jpg',
宽度=300,
高度=400,
)

或者,使用像
requests
这样的包来获取图像的
字节
,并将它们传递给
widgets.image
,而不需要
Ipython.display.image
实例。

它可以工作。非常感谢你。