-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice.cjs
More file actions
121 lines (121 loc) · 2.99 KB
/
Copy pathslice.cjs
File metadata and controls
121 lines (121 loc) · 2.99 KB
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
#!/usr/bin/env node
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const yargs = require('yargs');
const os = require('os');
const processImage_1 = require('./utils/processImage');
const processPath_1 = require('./utils/processPath');
async function main() {
// console.time('Done in');
// Parse command line arguments
const options = await yargs(process.argv.slice(2))
.option('f', {
alias: 'filename',
describe: 'Input image filename',
type: 'string',
coerce: (value) => {
if (Array.isArray(value)) {
value = value.join('');
}
return value;
},
})
.option('i', {
alias: 'folderPath',
describe: 'Input folder',
type: 'string',
coerce: (value) => {
if (Array.isArray(value)) {
value = value.join('');
}
return value;
},
})
.option('w', {
alias: 'width',
describe: 'Width of each slice',
type: 'number',
demandOption: true,
coerce: (value) => {
if (value < 1) {
throw new Error('width should not be lower than 1');
}
return Math.round(value);
},
})
.option('h', {
alias: 'height',
describe: 'Height of each slice',
type: 'number',
coerce: (value) => {
if (value !== undefined && value < 1) {
throw new Error('height should not be lower than 1');
}
return Math.round(value);
},
})
.option('d', {
alias: 'canvasWidth',
describe: 'Width of canvas for final output',
type: 'number',
coerce: (value) => {
if (value !== undefined && value < 1) {
throw new Error('canvasWidth should not be lower than 1');
}
return Math.round(value);
},
})
.option('g', {
alias: 'canvasHeight',
describe: 'Height of canvas for final output',
type: 'number',
coerce: (value) => {
if (value !== undefined && value < 1) {
throw new Error('canvasHeight should not be lower than 1');
}
return Math.round(value);
},
})
.option('s', {
alias: 'scale',
describe:
'Rescale the image up or down by this factor, after slicing, but before canvas resize',
type: 'number',
default: 1,
coerce: (value) => {
if (value <= 0) {
throw new Error('scale should be > 0');
}
return value;
},
})
.option('c', {
alias: 'cubic',
describe:
'Uses bicubic interpolation instead of nearest neighbour if rescaling',
type: 'boolean',
default: false,
})
.parse();
if (options.filename) {
// Process a single image
(0, processImage_1.sliceImage)(options);
} else if (options.folderPath) {
// Process all images in a folder, splitting the task into threads
let numCores = 1;
try {
numCores = os.cpus().length;
} catch (err) {
console.error(err);
}
numCores = Math.max(numCores - 1, 1); // Min 1
numCores = Math.min(numCores, 32); // Max 32
await (0, processPath_1.processPath)(options.folderPath, options, numCores);
} else {
console.error(
'Error: Requires either `filename` or `folderPath`. Run `slice --help` for help.',
);
}
// console.timeEnd('Done in');
}
main();