Florian DA
Florian DA

Reputation: 41

Split image file in two separate image

I have on my server an image file with an height of 50 000px. I want to save 2 files of 25 000px each (first and second part of the original image)

Any suggestions about how to do that ?

Thanks

Upvotes: 1

Views: 2184

Answers (1)

Abir Taheer
Abir Taheer

Reputation: 2783

The sharp image package might be useful for this scenario. More specifically the extract method.

I've added a link to the documentation but here's a possible implementation to split up the image.

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"
  });

I'm assuming you can reuse the original sharp image object to call the extract function twice. If not you might need to call the sharp constructor again.

Upvotes: 5

Related Questions