Node.js: Passing image as base64 to python with spawn

我与影子孤独终老i 提交于 2019-12-11 11:24:14

问题


I'm trying to pass an image as base64 to python for processing using spawn like so:

return new Promise(function(resolve, reject) {
      const pythonProcess = spawn('python',["./python.py", imageDataURI]);
      pythonProcess.stdout.on('data', (response) => {
        resolve(response);
      });
    });

But I'm getting error: Error: spawn E2BIG I guess it's too big to pass like this, any alternative ways to pass it to spawn?

Seems related:

Node / child_process throwing E2BIG


回答1:


Thanks to ottomeister's answer I did it like this:

In Node:

const pythonProcess = spawn('python',["script.py"]);

pythonProcess.stdin.write(data);
pythonProcess.stdin.end();
pythonProcess.stdout.on('data', (result) => {
    handleResult(result);
});

In python:

import fileinput

for line in fileinput.input():
    input +=line

# Process input

sys.stdout.write(result)
sys.stdout.flush()


来源:https://stackoverflow.com/questions/55794695/node-js-passing-image-as-base64-to-python-with-spawn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!