-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
134 lines (98 loc) · 3.32 KB
/
app.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var fs = require('fs'),
util = require('util'),
mime = require('mime'),
path = require('path'),
VerEx = require('verbal-expressions'),
imagemin = require('imagemin'),
imageminJpegRecompress = require('imagemin-jpeg-recompress'),
imageminMozjpeg = require('imagemin-mozjpeg'),
imageminPngquant = require('imagemin-pngquant');
function getRangeInfo (str) {
//https://github.com/VerbalExpressions/JSVerbalExpressions/wiki
var hasBrackets = VerEx().then('[').anything().then(']'),
result = str.match(hasBrackets),
range,
zeroPadding,
startVal,
endVal,
rv = null;
if (result) {
range = result[0].substr(1, result[0].length - 2).split('-');
rv = {
pattern: str.split('[' + range[0] + '-' + range[1] + ']').join('[replaceMe]'),
zeroPadding: range[0].length,
startValString: range[0],
endValString: range[1],
startVal: parseInt(range[0]),
endVal: parseInt(range[1]),
dirname: path.dirname(str),
ext: path.extname(str)
};
}
return rv;
}
function padDigits(number, digits) {
return Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number;
}
function base64Img(src, callback) {
var useImgMin = false;
if (useImgMin) {
imagemin([src], null, {
plugins: [
imageminJpegRecompress({quality: 'medium'}) // low, medium, high and veryhigh
]
}).then(function (files) {
var data = files[0].data.toString('base64'),
rv;
rv = util.format('data:%s;base64,%s', mime.lookup(src), data);
callback(rv);
});
} else {
var data = fs.readFileSync(src).toString('base64');
var rv = util.format('data:%s;base64,%s', mime.lookup(src), data);
callback(rv);
}
}
if(process.argv.length > 2) {
var pathPattern = process.argv[2],
rangeInfo = getRangeInfo(pathPattern),
len,
outputFile,
framesImg = [],
imgPath,
b64;
if (getRangeInfo) {
len = rangeInfo.endVal - rangeInfo.startVal;
for (var i = 0; i < len + 1; i++) {
imgPath = rangeInfo.pattern.split('[replaceMe]').join(padDigits(i, rangeInfo.zeroPadding));
// b64 = base64Img(imgPath);
// framesImg.push(b64);
base64Img(imgPath, function (data) {
framesImg.push(data);
if (framesImg.length === len) {
outputFile = pathPattern.split(rangeInfo.ext).join('.seq');
var json = {
frames: framesImg
};
fs.writeFile(outputFile, JSON.stringify(json), function(err) {
if(err) {
console.log(err);
} else {
console.log('Completed!');
}
});
}
});
}
} else {
console.error('Image path pattern unvalide, Use this form: seqs/mySequence_[00000-00002].png');
}
} else {
console.error('Not enough parameters supplied!');
}
/**
Usage:
node app.js seqs/keySeq/Volvo_Luxury_Keyfob_[00000-00015].png
node app.js seqs/keySeq/Volvo_Luxury_Keyfob_[00000-00002].png
node app.js seqs/keySeq/Volvo_Luxury_Keyfob_[00000-00002].jpg
**/