I am trying to write some code that will take an image that is uploaded by a user and resize it. The code below works for very small images, but when I try a larger image I receive the error spawn ENAMETOOLONG. I believe this is because the base64 string is much larger. What could I do to send the base64 string to my python script regardless of length ?
server.js
const _arrayBufferToBase64 = (buffer) => {
return Buffer.from(buffer).toString('base64');
};
// spawn new child process to call the python script
const python = spawn('python', ['./python/image_handler.py', _arrayBufferToBase64(imgData), filename]);
// collect data from script
python.stdout.on('data', function (pydata) {
console.log('Pipe data from python script ...');
});
python.on('close', (code) => {
console.log(`child process close all stdio with code ${code}`);
});
image_handler.py
img_b64 = sys.argv[1]
img_bytes = base64.b64decode(img_b64) # im_bytes is a binary image
img_file = io.BytesIO(img_bytes) # convert image to file-like object
img = Image.open(img_file) # img is now PIL Image object
img.thumbnail((300, 300))# resize image
img.save(sys.argv[2]) # Save Path
print('Finished')
sys.stdout.flush()