1

I wrote a node.js script to use jimp to get all image files from a directory, (which I take as input), run some manipulations on that and then save them in the target directory (another input) as filename.suffix.extension. I take suffix also as input.

But I only see the last file from the list that I collect as to be present in the target directory.

// imports
var Jimp = require('jimp');
const fs = require('fs')

// inputs
dir = process.argv[2]
target = process.argv[3]
suffix = process.argv[4]

// collect files
let dirCont = fs.readdirSync( dir );
const files = dirCont.filter( ( elm ) => /.*\.(png|jpg)/gi.test(elm) );

// run jimp on each file and write to target directory
for (file in files)
{  
    target_file = target+files[file].replace(/\.[^/.]+$/, "")+'.'+suffix+files[file].match(/\.[^/.]+$/)
    Jimp.read(dir+'/'+files[file]).then(function (file) {
        return file.resize(256, 256)     // resize
             .quality(60)                 // set JPEG quality
             .greyscale()                 // set greyscale
             .write(target_file); // save
    })

}

I run the entire thing using grunt.

J. Doe
  • 33
  • 3

0 Answers0