在 Node.js (AWS Lamda) 中使用 Jimp 将 .jpg 图像转换为 png

发布于 2025-01-12 02:38:47 字数 549 浏览 6 评论 0原文

我正在编写一个 AWS Node.js Lambda 函数(使用无服务器)来在给定初始 URL 的情况下转换不同格式的图像(即 JPG--> PNG)。我正在使用 Jimp 库,根据文档,该库使用以下代码实现此功能:

Jimp.read(JPG_URL, function (err, image) {
  if (err) {
    console.log(err)
  } else {
    image.write("new-image.png")
  }
})

现在,在我正在使用的 Lambda 函数中:

let img_data = await Jimp.read(JPG_URL);

效果很好,实际上我可以使用 img_data 来执行不同的转换(即img_data.greyscale())。问题是(据我所知)Lambda 的文件系统是只读的,而 Jimp 似乎不支持直接转换为变量的方法。

如何在不依赖文件系统的情况下执行转换?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风苍溪 2025-01-19 02:38:47

您可以尝试锐利,如下所示

const sharp = require("sharp");

async function convert() {
  try {
    await sharp("myfile.jpeg").png({ palette: true }).toBuffer()
  } catch (error) {
    console.log(error);
  }
}

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);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文