javascript - 从坐标存储在缓冲区中的Javascript裁剪图像
问题描述
我找到了使用画布在 javascript 中裁剪图像的代码,但我需要裁剪一个从未实际显示在任何地方的图像,因为我只需要从图像中获取数据。有一个屏幕截图,然后需要从特定坐标裁剪缓冲的屏幕截图图像。
图像只是存储在一个以 开头的变量中let img = new Image()
。如何修改此代码以裁剪仅存储在缓冲区中的图像?
function resizeImage(url, width, height, x, y, callback) {
var canvas = document.createElement("canvas");
var context = canvas.getContext('2d');
var imageObj = new Image();
// set canvas dimensions
canvas.width = width;
canvas.height = height;
imageObj.onload = function () {
context.drawImage(imageObj, x, y, width, height, 0, 0, width, height);
callback(canvas.toDataURL());
};
imageObj.src = url;
}
解决方案
由于没有人回答,我将发布我发现的内容。最后,我使用库 Sharp 的提取功能实现了我所需要的。
img.onload = resizeImg;
img.src = 'image.png';
function resizeImg() {
this.path = this.path = 'image.png';
sharp(this.path)
.resize(this.width * 2, this.height * 2)
.extract({ width: 100, height: 20, left: 250, top: 100 })
.toBuffer({ resolveWithObject: true })
.then(({ data, info }) => {
//process data
})
}
推荐阅读
- python - TensorFlow/Keras 模型 __call__ 在 GPU 上运行时变得越来越慢
- mysql - 如何在单个原子操作中将临时表转换为永久表?
- javascript - 我使用 split 和 join 的替换代码仅适用于单个单词输入,而不适用于整个单词
- python - 使用参数化和 xfail 时出现 pytest 错误
- r - 如果我使用 {.tabset},如何在我的 Flexdashboard 中添加另一行?R
- python - 在 Python 中子类化两个类?
- machine-learning - 获取错误作为数据的 DataFrame.dtypes 必须是 int、float、bool 或 categorical
- javascript - Mailchimp Api 无法获取我发送的 email_address 字段
- python - 美丽的汤:从列表中提取文本和网址,但仅在特定标题下
- java - 无法在 Ubuntu 上运行我的 springboot 项目