javascriptblobdata-conversiontesseract.jsimagedata

Convert ImageData to blob in JS?


I have an ImageData object but Tesseract.js only takes blob objects. How can I convert the ImageData to a blob as performantly as possible?


Solution

  • Referring here, the code should look like -

    const ImageDataToBlob = function(imageData){
      let w = imageData.width;
      let h = imageData.height;
      let canvas = document.createElement("canvas");
      canvas.width = w;
      canvas.height = h;
      let ctx = canvas.getContext("2d");
      ctx.putImageData(imageData, 0, 0);        // synchronous
    
      return new Promise((resolve) => {
            canvas.toBlob(resolve); // implied image/png format
      });
    }