Crop the polygon image with red border

20 Views Asked by At

i am using jimp to change the pixels to white which are outside the polygon border but the problem is it change the all pixels excluding the border color.

const Jimp = require("jimp");

const inputImagePath = "mapimage.png"; // input image path
const outputImagePath = "mapresult.png";  //output image path

Jimp.read(inputImagePath)
  .then ((image) => {
   image.scan(0,0,image.bitmap.width,
    image.bitmap.height,
    function (x,y,idx){
      const [red , green , blue] = this.bitmap.data.slice(idx,idx+3)

      if(red > 170){
        //pixel is inside the border leave it uncahnged
      }
      else{
        // pixel is outside the border, set it to white
        this.bitmap.data[idx] = 255; // set red to 255
        this.bitmap.data[idx + 1] = 255; // set green to 255
        this.bitmap.data[idx + 2] = 255; // set blue to 255
        this.bitmap.data[idx + 3] = 255; // set alpha to 255 fully opaque
      }
    }
  );

  // save the modified image
  return image.writeAsync(outputImagePath);
  }).then(() => {
    console.log("image processing completed!")
  }).catch((err)=>{
    console.log(err);
  })

image is input image output which i currently getting output currently

desired output is desired output is

i want cropped image only polygon or image having white pixels outside the border.please assist.

0

There are 0 best solutions below