-
Notifications
You must be signed in to change notification settings - Fork 0
/
stylize.mjs
55 lines (43 loc) · 1.59 KB
/
stylize.mjs
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
import axios from 'axios';
import glob from 'glob';
import fs from 'fs';
import FormData from 'form-data';
const MAX_IMAGE_SIZE = 5000000;
async function stylize(imagePath) {
const image = {
data: fs.createReadStream(imagePath),
mimetype: 'image/jpeg',
name: imagePath,
};
const data = new FormData({ maxDataSize: MAX_IMAGE_SIZE });
data.append('image', image.data, image.name);
const headers = {
'ApiKeyId': `${ process.env.STYLIZATION_SDK_API_KEY_ID }`,
'ApiKey': `${ process.env.STYLIZATION_SDK_API_KEY }`,
};
const pipeline = [{
'effectId': 'default/toon/definition/toon/1',
'preset': 'realistic',
}];
data.append('pipeline', JSON.stringify(pipeline));
console.log(`post '${ imagePath }' for stylization ...`);
const response = await axios({
method: 'post',
url: `${ process.env.STYLIZATION_SDK_API_URL }${ process.env.STYLIZATION_SDK_API_ROUTE }`,
data: data,
headers: Object.assign(Object.assign({}, headers), data.getHeaders()),
responseType: 'stream',
}).then(function (response) {
response.data.pipe(fs.createWriteStream(imagePath));
console.log(`stylization '${ imagePath }' succeeded`);
}).catch(function (reason) {
response.data.pipe(fs.createWriteStream(imagePath));
console.log(`stylization of '${ imagePath }' failed`);
console.log(`reason: ${ reason }`);
});
}
glob("source/img/**/header-*.jpg", function (er, files) {
files.forEach((value, index) => {
stylize(value);
});
});