【问题标题】:POST an image and display it in dashboard in Node-red发布图像并以 Node-red 显示在仪表板中
【发布时间】:2019-08-01 19:54:09
【问题描述】:

我的目标是通过 Python 中的 POST 请求发送一张图片,并将其显示在 Node Red 的仪表板中,但是一旦发送,我就卡在了读取图像的步骤中。

这是我用来发送文件的一段代码:

directory = os.path.join(DIRECTORY_IMAGES, id)
files = {'image': open(directory,'rb')}
requests.post('http://XXX.XXX.X.XX:1880/IMAGE', files = files)

在 Node-red 中,我使用的块如下:

消息已收到,但我无法从中取出图片。正如我在其他帖子中所读到的,msg.payload 设置为req.files[0].buffer,编码为Base64 并在模板中使用<img width="16" height="16" src="files:image;base64,{{msg.payload}}" /> 显示,但这不会显示图像。

[{"id":"797939a8.a91a58","type":"http response","z":"ddfa6621.2e7168","name":"","statusCode":"","headers":{},"x":1290,"y":2280,"wires":[]},{"id":"d5b145ce.a712f8","type":"ui_template","z":"ddfa6621.2e7168","group":"9beeae56.34305","name":"Display image","order":4,"width":"7","height":"6","format":"<img width=\"16\" height=\"16\" src=\"files:image;base64,{{msg.payload}}\" />\n","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":1680,"y":2220,"wires":[[]]},{"id":"5e30e496.958b9c","type":"change","z":"ddfa6621.2e7168","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"req.files[0].buffer","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1300,"y":2220,"wires":[["2f260e60.d12642"]]},{"id":"2f260e60.d12642","type":"base64","z":"ddfa6621.2e7168","name":"Encode","x":1500,"y":2220,"wires":[["d5b145ce.a712f8"]]},{"id":"18c71628.986f4a","type":"http in","z":"ddfa6621.2e7168","name":"","url":"IMAGE","method":"post","upload":true,"swaggerDoc":"","x":1050,"y":2220,"wires":[["5e30e496.958b9c","797939a8.a91a58"]]},{"id":"9beeae56.34305","type":"ui_group","z":"","name":"Última pierna procesada","tab":"223d38a0.9cbaa8","order":8,"disp":true,"width":"7","collapse":false},{"id":"223d38a0.9cbaa8","type":"ui_tab","z":"","name":"CALIDAD PULPAS CINTA 0","icon":"dashboard","disabled":false,"hidden":false}]

我错过了什么?

【问题讨论】:

  • 看看这个LINK,看看是否可行
  • 我猜问题出在 Node-red 部分,我正在发送数据并收到它,但没有采取进一步行动
  • 您是否已在流程中附加了调试节点,以确保您得到预期的结果。还要检查仪表板的浏览器控制台。

标签: python node-red


【解决方案1】:

img src 标签应该以data:image/jpeg;base64, 开头(假设是JPEG 图像)

您应该将 image/jpeg 替换为图像的 mime 类型,例如image/png

[{"id":"9dff2c2e.66ea1","type":"http response","z":"c09a8743.c89388","name":"","statusCode":"","headers":{},"x":390,"y":180,"wires":[]},{"id":"68e0a720.f29498","type":"ui_template","z":"c09a8743.c89388","group":"efcf5006.15dae","name":"Display image","order":4,"width":"7","height":"6","format":"<img width=\"16\" height=\"16\" src=\"data:image/jpeg;base64,{{msg.payload}}\" />\n","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":800,"y":120,"wires":[[]]},{"id":"2fb71bea.a5d1e4","type":"http in","z":"c09a8743.c89388","name":"","url":"IMAGE","method":"post","upload":true,"swaggerDoc":"","x":210,"y":120,"wires":[["9dff2c2e.66ea1","a87b8d6.a9b0c7"]]},{"id":"a87b8d6.a9b0c7","type":"change","z":"c09a8743.c89388","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"req.files[0].buffer","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":120,"wires":[["4bcddbb.38c2624"]]},{"id":"4bcddbb.38c2624","type":"base64","z":"c09a8743.c89388","name":"Encode","x":620,"y":120,"wires":[["7d7586e7.205398","68e0a720.f29498"]]},{"id":"7d7586e7.205398","type":"debug","z":"c09a8743.c89388","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":60,"wires":[]},{"id":"efcf5006.15dae","type":"ui_group","z":"","name":"Última pierna procesada","tab":"65324def.436554","order":8,"disp":true,"width":"7"},{"id":"65324def.436554","type":"ui_tab","z":"","name":"CALIDAD PULPAS CINTA 0","icon":"dashboard"}]

由以下 python 触发:

import requests

files = {'image': open('/Users/hardillb/temp/photos/DSC_7188.JPG', 'rb')}
requests.post('http://localhost:1880/IMAGE', files = files)

【讨论】:

  • 我已经这样做了,但我看到的唯一调试消息是 HTTPInMultipart 节点中的以下内容:“MulterError: Unexpected field”。我将字段设置为[{"name":"myFile"}],我发送的文件设置为files = {'name':('myFile', open('image.jpg', 'rb'), 'image/jpg')}
  • 我刚刚使用我建议的更改对此进行了测试,并且效果很好。我已经编辑了答案以包含我的流程版本(唯一的更改是在模板节点中 img 标记的 src 属性中添加前缀。
  • @hardillb,谢谢你的回答。它对我有用,所以被赞成(像往常一样你的答案)。只是想知道 ui_template 中的属性 width="16" height="16" 是否有任何用途。图像大小似乎仅由 ui_template 小部件大小控制。如果我想从 Node-RED 实例发送 HTTP 帖子,我应该将标题“Content-Type”设置为“multipart/form-data”还是“image/jpeg”?
  • 您也可以接受答案(点击勾选)。至于图像的大小,最好的办法是使用 Chrome 的检查功能来检查正在应用的 CSS 样式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
  • 2017-04-13
  • 1970-01-01
  • 1970-01-01
  • 2014-12-12
  • 1970-01-01
  • 2018-01-18
相关资源
最近更新 更多