jimp-dev / jimp

An image processing library written entirely in JavaScript for Node, with zero external or native dependencies.
http://jimp-dev.github.io/jimp/
MIT License
14.04k stars 760 forks source link

Processing multiple Images and calculating number of dark and white pixels. [SLOW] #756

Closed mateuszBetlej4 closed 1 year ago

mateuszBetlej4 commented 5 years ago

Expected Behavior

It reads a folder with multiple images (706) and calculates the dark and white pixels and convets the number of white pixels into a percentage.

Current Behavior

The code does what I need it to do except it's very slow. I have noticed that it tries to open all images at once and just then proceed to the calculations. I have a 8gb ram laptop. While the code reads the images the ram peaks up to 6gb and the program freezes for about 10min before it proceeds with the calculations.

var Jimp = require("jimp");
var fs =require('fs');

fs.readdir("./unZipped/", function(err, filenames) {
    if (err) {
      onError(err);
      return;
    }
    filenames.forEach(function(filename) {
        if (filename.indexOf('.png')>-1){
            //console.log(filename)
            processimage(filename)
        }
        else{
        var x=0;

        }
    });
  });
var processimage=function (imageFile){
    Jimp.read('./unZipped/'+imageFile, (err, lenna) => {
        console.log("in read");
        if (err) throw err;
        var iwidth = lenna.bitmap.width ;
        var iheight = lenna.bitmap.height ;
        //console.log("image width: "+ iwidth);

        //console.log("image height: "+ iheight);
    var darkcount =0;
        for (var i =0; i<iwidth;i++){
            for (var j=0; j<iheight;j++){

                var col = lenna.getPixelColor(i, j); 
                //console.log ("i "+ i + " j " +j+ "col: "+ col);

                if (col===255)
                {
                    darkcount++;

                }
            }
        }
        var totalSize=i*j
        var percentlight2Dark= (totalSize-darkcount)/totalSize;
        console.log (imageFile+" darkcount "+ darkcount + " total count  " +i*j+ " %age light dark : "+percentlight2Dark*100);
    });
}

maybe someone could help me to read the images one at a time and do the calculation and carry on with the rest of the images in the folder so that it doesn't fill up the memory. Thanks

notliam commented 5 years ago

Hi, this isn't really an issue with JIMP as much as it is just a coding issue. You are correct in your assumption that the program you wrote will by processing all the files at the same time, and this is likely the cause of your memory and CPU issues. What you want to do is instead of calling processimage inside the readDir function, instead add them to an array. Once the directory has been read and the files all found, start work on the image processing - call processimage on the first file in the array, and do not call again until the process is complete (simple way to do this would be to have some flag variables set when the process starts and ends, and maybe just have a setInterval checking the flags. A nicer way is to use a loop and async.

This will still take a long time as using getPixelColor is not incredibly fast, but this will at least allow the program to not hog all your laptops memory so you can at least do other stuff in the mean time. I also understand that if you opened this 25 days ago, you probably have resolved this or moved past this issue!