forked from jcmacur/medium-tutorial-nft-generation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.js
More file actions
113 lines (88 loc) · 3.23 KB
/
generator.js
File metadata and controls
113 lines (88 loc) · 3.23 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
require('dotenv').config();
const Jimp = require('jimp');
const fs = require('fs');
const pinataSDK = require('@pinata/sdk');
const pinata = pinataSDK(process.env.PINATA_KEY, process.env.PINATA_API_SECRET);
const Traits = require('./traits');
const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
const build = async(index, onComplete) => {
const _path = '/Users/jcmacur/Documents/Projects/nftgenerator/';
var _traits = [];
const background = Traits.getBackground();
const backgroundJimp = await Jimp.read(_path+'/Traits/Background/Background_'+background+'.png');
_traits.push({
'trait_type': 'Background',
'value': background
});
var _composedImage = backgroundJimp;
const bodyAndHead = Traits.getBodyAndHead();
const bodyJimp = await Jimp.read(_path+'/Traits/Body/Body_'+bodyAndHead+'.png');
_traits.push({
'trait_type': 'Body',
'value': bodyAndHead
});
_composedImage.blit(bodyJimp, 0, 0);
const outfit = Traits.getOutfit();
const outfitJimp = await Jimp.read(_path+'/Traits/Outfit/Outfit_'+outfit+'.png');
_traits.push({
'trait_type': 'Outfit',
'value': outfit
});
_composedImage.blit(outfitJimp, 0, 0);
const headJimp = await Jimp.read(_path+'/Traits/Head/Head_'+bodyAndHead+'.png');
_traits.push({
'trait_type': 'Head',
'value': bodyAndHead
});
_composedImage.blit(headJimp, 0, 0);
const nose = Traits.getNose();
const noseJimp = await Jimp.read(_path+'/Traits/Nose/Nose_'+nose+'.png');
_traits.push({
'trait_type': 'Nose',
'value': nose
});
_composedImage.blit(noseJimp, 0, 0);
const mouth = Traits.getMouth();
const mouthJimp = await Jimp.read(_path+'/Traits/Mouth/Mouth_'+mouth+'.png');
_traits.push({
'trait_type': 'Mouth',
'value': mouth
});
_composedImage.blit(mouthJimp, 0, 0);
const eyes = Traits.getEyes();
const eyesJimp = await Jimp.read(_path+'/Traits/Eyes/Eyes_'+eyes+'.png');
_traits.push({
'trait_type': 'Eyes',
'value': eyes
});
_composedImage.blit(eyesJimp, 0, 0);
const sunglasses = Traits.getSunglasses();
const sunglassesJimp = await Jimp.read(_path+'/Traits/Sunglasses/Sunglasses_'+sunglasses+'.png');
_traits.push({
'trait_type': 'Sunglasses',
'value': sunglasses
});
_composedImage.blit(sunglassesJimp, 0, 0);
const headwear = Traits.getHeadwear();
const headwearJimp = await Jimp.read(_path+'/Traits/Headwear/Headwear_'+headwear+'.png');
_traits.push({
'trait_type': 'Headwear',
'value': headwear
});
_composedImage.blit(headwearJimp, 0, 0);
await _composedImage.write('Output/images/'+index+'.png');
await sleep(20); //We give some time for the image to be actually saved in our files
const _readableStream = await fs.createReadStream(_path + '/Output/images/'+index+'.png');
const _ipfs = await pinata.pinFileToIPFS(_readableStream);
await fs.writeFileSync('Output/'+index, JSON.stringify({
"name": "My NFT #"+index,
"traits": _traits,
"image": "https://ipfs.io/ipfs/"+_ipfs.IpfsHash
}))
onComplete();
}
module.exports = {
build
}