I'm writing an AWS Node.js Lambda function (using Serverless) to convert images across different formats (i.e. JPG--> PNG) given an initial URL. I'm using the Jimp library which, according to the documentation, implements this functionality with the code:
Jimp.read(JPG_URL, function (err, image) {
if (err) {
console.log(err)
} else {
image.write("new-image.png")
}
})
now, in my Lambda function I'm using:
let img_data = await Jimp.read(JPG_URL);
which works well, indeed I can use img_data
to perform different transformations (i.e. img_data.greyscale()
). The problem is that (AFAIK) Lambda's filesystem is read-only and Jimp doesn't seem to support a way to convert directly to a variable.
How can I perform the conversion without relying on the filesystem?
You may try sharp like below
const sharp = require("sharp");
async function convert() {
try {
await sharp("myfile.jpeg").png({ palette: true }).toBuffer()
} catch (error) {
console.log(error);
}
}