【发布时间】:2019-09-17 19:15:36
【问题描述】:
我正在尝试实现 Node.js 和 Python 之间的通信。对于这个任务,我使用 Node.js 的 python-shell NPM 模块来运行 Python 脚本并读取打印输出。我想在 Python 上做一些 OpenCV 图像处理的东西,将图像发送到 Node.js 并在应用程序上提供它。
这里是 Node.js 部分:
let {PythonShell} = require('python-shell')
let options = {
mode: 'text',
pythonOptions: ['-u'], // get print results in real-time
args: ['value1', 'value2', 'value3']
};
PythonShell.run('engine.py', options, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
/* var fs = require("fs");
fs.writeFile("arghhhh.jpeg", Buffer.from(results, "base64"), function(err) {}); */
console.log(results.toString())
});
这里是 Python 部分:
from PIL import Image
import cv2 as cv2
import base64
source = cv2.imread("60_3.tif", cv2.IMREAD_GRAYSCALE)
# tried making it a PIL image but didn't change anything
# source = Image.fromarray(source)
print(base64.b64encode(source))
理论上一切看起来都不错,但是,我尝试在 Node.js 端编写图像,但无法打开图像。还检查了两个字符串的大小,Node.js 端有 3 个字符差异。 我是否需要在两种语言之间共享一个简单的图像?
【问题讨论】:
标签: python node.js opencv base64 buffer