-
Notifications
You must be signed in to change notification settings - Fork 1
/
image-gen.js
110 lines (95 loc) · 3.17 KB
/
image-gen.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
require('dotenv').config();
const fs = require('fs');
var Jimp = require('jimp');
const traits = require('./traits.json');
const extension = 'png';
const imgCount = process.env.IMAGE_COUNT;
function getFilePath(trait, index) {
const fileName = traits[trait][index].name;
const count = traits[trait][index].count;
if (fileName === 'NONE') {
return undefined;
}
return `traits/${trait}/${fileName}#${count}.${extension}`;
}
async function checkAllTraitInfo() {
for (const trait of Object.keys(traits)) {
let imageCount = 0;
for (let i = 0; i < traits[trait].length; i++) {
imageCount = imageCount + traits[trait][i].count;
const fileName = getFilePath(trait, i);
if (fileName === undefined || fs.existsSync(fileName)) {
} else {
console.log("DOES NOT exist:", fileName, trait, i);
exist(1);
}
}
if (imageCount.toString() !== imgCount) {
console.log("Total count is not correct:", trait, imageCount);
exist(1);
}
}
console.log("All are correct!!!");
}
async function generateImage(assignedTraits, index) {
let baseImg = await Jimp.read(
getFilePath(Object.keys(assignedTraits)[0], assignedTraits[Object.keys(assignedTraits)[0]][index])
);
for (const trait of Object.keys(assignedTraits).slice(1)) {
const path = getFilePath(trait, assignedTraits[trait][index]);
if (path) {
const img = await Jimp.read(path);
baseImg.composite(img, 0, 0);
}
}
await baseImg.write(`images/${index + 0}.png`);
}
function shuffle(array) {
var tmp, current, top = array.length;
if (top) while (--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
}
function makeInfo(assignedTraits) {
const info = [];
for (let i = 0; i < imgCount; i++) {
const temp = {};
for (const trait of Object.keys(traits)) {
const name = traits[trait][assignedTraits[trait][i]].name;
if (name !== 'NONE') {
temp[trait] = name;
}
}
temp.name = `${process.env.NAME} #${i + 0}`;
info.push(temp);
}
fs.writeFile(`info.json`, JSON.stringify(info), function (err) {
if (err) throw err;
});
}
async function main() {
const assignedTraits = {};
for (const trait of Object.keys(traits)) {
assignedTraits[trait] = [];
for (let i = 0; i < traits[trait].length; i++) {
assignedTraits[trait] = [...assignedTraits[trait], ...Array(traits[trait][i].count).fill(i)];
}
if (assignedTraits[trait].length != imgCount) {
console.log(`Trait [${trait}] info is not correct.`);
return;
}
assignedTraits[trait] = shuffle(assignedTraits[trait]);
}
for (let i = 0; i < imgCount; i++) {
await generateImage(assignedTraits, i);
console.log(`Generated image #${i}`);
}
makeInfo(assignedTraits);
console.log('Finished');
}
// checkAllTraitInfo();
main();