forked from UniqueNetwork/nft-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep2-image-generator.js
More file actions
68 lines (49 loc) · 1.47 KB
/
step2-image-generator.js
File metadata and controls
68 lines (49 loc) · 1.47 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
const fs = require('fs');
const config = require('./config');
const { spawn, Pool, Worker } = require('threads');
const attributes = config.attributes;
let faces;
function getImageData(arr) {
let images = [];
for (let i=0; i < arr.length; i++) {
if (arr[i] > 0) {
const img = {
src: `${config.imagePartsFolder}/${attributes[i].name}${arr[i]}.png`,
offsetX: (i == 0) ? 0 : -config.imageWidth,
offsetY: 0,
}
images.push(img);
}
}
return images;
}
function printAttributes(i) {
let attrs = '[' + faces[i] + '] => ';
for (let j=0; j<attributes.length; j++) {
if (faces[i][j] > 0) {
attrs += attributes[j].values[faces[i][j]-1] + ", ";
}
}
console.log(`Attributes for NFT ${i+1}:`, attrs);
}
async function generateImages() {
const pool = Pool(() => spawn(new Worker('./scripts/generate-image.worker')), config.imagesInParallel);
for (let i=0; i<faces.length; i++) {
const face = faces[i];
const output = `${config.outputFolder}/${config.imagePrefix}${i+1}.png`;
const images = getImageData(face);
pool.queue(async (generateImage) => {
const num = await generateImage({ images, output, num: i });
printAttributes(num);
});
}
await pool.completed()
await pool.terminate()
}
async function main() {
faces = JSON.parse(fs.readFileSync(`${config.outputFolder}/${config.outputJSON}`));
console.time('images');
await generateImages();
console.timeEnd('images');
}
main();