首页 > 解决方案 > 将图像文件拆分为两个单独的图像

问题描述

我的服务器上有一个高度为 50 000 像素的图像文件。我想保存 2 个 25 000px 的文件(原始图像的第一和第二部分)

关于如何做到这一点的任何建议?

谢谢

标签: javascriptnode.jsbackend

解决方案


清晰的图像包可能对这种情况有用。更具体地说是提取方法

我添加了指向文档的链接,但这里有一个拆分图像的可能实现。

const sharp = require("sharp");

const originalFilename = "image.jpg";


const image = sharp(originalFilename);

// this is just a placeholder
const imageWidth = 500;

image
  .extract({ left: 0, top: 0, width: imageWidth, height: 25000 })
  .toFile("top.jpg", function(err) {
    // Save the top of the image to a file named "top.jpg"
  });

image
  .extract({ left: 0, top: 25000, width: imageWidth, height: 25000 })
  .toFile("bottom.jpg", function(err) {
    // Save the bottom of the image to a file named "bottom.jpg"
  });

我假设您可以重复使用原始清晰图像对象来调用提取函数两次。如果不是,您可能需要再次调用尖锐的构造函数。


推荐阅读