如何在nodejs中裁剪屏幕截图或图像(.png)?

sri*_*tas 9 javascript imagemagick image-processing node.js easyimage

  1. 考虑一个图像文件 .png
  2. 假设您有某些元素的 xy 线,例如 x:400、y:500
  3. 假设您有要裁剪的图像大小:宽度:50,高度:20

我有以下来自nodejs pack easyimage 的片段,并且我也安装了ImageMagick。 当我运行下面的代码时,它只是通过但无法裁剪图像。

easyimage.crop(
  {
    src: "F:/screenshot.png",  // Contains fullscreen image
    dst: "F:/screenshot.png",  // New image with cropped name
    x: 400,
    y: 500,
    cropwidth: 50,
    cropheight: 20,
    gravity: "North-West",
  },
  (err, stdout, stderr) => {
    if (err) throw err
  }
)
Run Code Online (Sandbox Code Playgroud)

Tua*_*ran 14

我用sharp这个,效果很好

尝试这个

const sharp = require('sharp')

sharp('./kangta.jpg')
    .extract({ left: 0, top: 0, width: 100, height: 100 })
    .toFile('./kangta.new.jpg', function (err) {
        if (err) console.log(err);
    })

Run Code Online (Sandbox Code Playgroud)

尖锐: https: //www.npmjs.com/package/sharp