我正在尝试实现 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())
});
Run Code Online (Sandbox Code Playgroud)
这是Python部分:
from …Run Code Online (Sandbox Code Playgroud)