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

时光怂恿深爱的人放手 提交于 2020-01-15 11:16:32

问题


I'm trying to achieve communication between Node.js and Python. For this task, I'm using Node.js's python-shell NPM module to run a Python script and read the print output. I want to do some OpenCV image processing stuff on Python, send the image to Node.js and serve it on an application.

Here is the Node.js part:

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:

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. Also checked sizes of the both strings and there was 3 character difference on Node.js side. Do I need to do something else in between to share a simple image between two languages?


回答1:


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. Also, the image as bytes needs to be encoded and decoded as 'ascii' to be read on the NodeJS part.




回答2:


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=.

Try running from your shell

python3 engine.py


来源:https://stackoverflow.com/questions/57980693/cant-read-base64-encoded-image-in-node-js-which-is-sent-from-python

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