-
Notifications
You must be signed in to change notification settings - Fork 0
/
JPGtoPng.js
48 lines (46 loc) · 1.62 KB
/
JPGtoPng.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const Jimp= require("jimp");
const fs = require("fs");
const directoryPath = "./img"
fs.readdir(directoryPath, function (err, files) {
//handling error
if (err) {
return //comsole.log('Unable to scan directory: ' + err);
}
//listing all files using forEach
files.forEach(function (file) {
// Do whatever you want to do with the file
if(/.jfif/.test(file))
{
//comsole.log(file)
//We will first read the JPG image using read() method.
Jimp.read(directoryPath+"/"+file, function (err, image) {
//If there is an error in reading the image,
//we will print the error in our terminal
if (err) {
console.log(err)
}
//Otherwise we convert the image into PNG format
//and save it inside images folder using write() method.
else {
image.write(directoryPath+"/"+file.replace(/.jfif/,".png"))
fs.unlink(directoryPath+"/"+file,function(err){
if(err) return //comsole.log(err);
console.log('file deleted successfully');
});
}
})
}
// if(/removebg/.test(file)){
// console.log(file)
// fs.rename(directoryPath+"/"+file, directoryPath+"/"+file.replace("-removebg-preview",""), function(err) {
// if ( err ) console.log('ERROR: ' , err)
// });
// }
// if(/_/.test(file)){
// console.log(file)
// fs.rename(directoryPath+"/"+file, directoryPath+"/"+file.replace("_",""), function(err) {
// if ( err ) console.log('ERROR: ' + err);
// });
// }
});
});