简体   繁体   English

无法读取从 Python 发送的 Node.js 中的 Base64 编码图像

[英]Can't read Base64 encoded image in Node.js which is sent from Python

I'm trying to achieve communication between Node.js and Python.我正在尝试实现 Node.js 和 Python 之间的通信。 For this task, I'm using Node.js's python-shell NPM module to run a Python script and read the print output.对于此任务,我使用 Node.js 的 python-shell NPM 模块来运行 Python 脚本并读取打印输出。 I want to do some OpenCV image processing stuff on Python, send the image to Node.js and serve it on an application.我想在 Python 上做一些 OpenCV 图像处理的东西,将图像发送到 Node.js 并在应用程序上提供它。

Here is the Node.js part:这是 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())
});

Here is the Python part:这是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))

Everything looks good in theory, however, I tried to write the image on Node.js side and I can't open the image.理论上一切看起来都不错,但是,我尝试在 Node.js 端编写图像,但无法打开图像。 Also checked sizes of the both strings and there was 3 character difference on Node.js side.还检查了两个字符串的大小,Node.js 端有 3 个字符差异。 Do I need to do something else in between to share a simple image between two languages?我是否需要在两者之间做其他事情来在两种语言之间共享一个简单的图像?

import cv2 as cv2
import base64

source = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
success, encoded_image = cv2.imencode('.png', source)
content = encoded_image.tobytes()
print(base64.b64encode(content).decode('ascii'))

This is how I figured it out.我就是这样想出来的。 Encoding image with OpenCV's imencode method and converting it to bytes with .tobytes() is curial.使用 OpenCV 的 imencode 方法编码图像并使用 .tobytes() 将其转换为字节是古怪的。 Also, the image as bytes needs to be encoded and decoded as 'ascii' to be read on the NodeJS part.此外,作为字节的图像需要编码和解码为“ascii”才能在 NodeJS 部分读取。

You're most probably running your script with python 2, but the library you're using is using python3 , and your string will look something like b'aGVsbG8=' instead of aGVsbG8= .您很可能使用python 2 运行脚本,但您使用的库使用的是python3 ,并且您的字符串看起来像b'aGVsbG8='而不是aGVsbG8=

Try running from your shell尝试从 shell 运行

python3 engine.py

python code # cv.py python代码#cv.py

import cv2 as cv2
import base64

source = cv2.imread('0.png', cv2.IMREAD_GRAYSCALE)
success, encoded_image = cv2.imencode('.png', source)
content = encoded_image.tobytes()
print(base64.b64encode(content).decode('ascii'))

nodejs code节点代码

const spawn = require('child_process').spawn;
const fs = require('fs');

const process = spawn('python', ['./cv.py']);

process.stdout.on('data', data => {
  console.log(data.toString()); 
  fs.writeFile("test.png", Buffer.from(data.toString(), "base64"), function(err) {});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM