forked from khrome/ascii-art
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
80 lines (77 loc) · 2.33 KB
/
generate.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
var art = require('./ascii-art');
var difference = require('color-difference');
var arrays = require('async-arrays');
var fs = require('fs');
function moreAccurateColor(r1, g1, b1, r2, g2, b2){
return difference.compare(
'#'+r1.toString(16)+g1.toString(16)+b1.toString(16),
'#'+r2.toString(16)+g2.toString(16)+b2.toString(16)
);
}
art.Figlet.fontPath = './Fonts/';
var needsAccurateColor = [
'./Images/sewer-grate.jpg',
'./Images/max-headroom.jpg',
'./Images/truth.png',
'./Images/rene-cigler.jpg',
'./Images/tony_harrison.jpg'
];
var alternateAlphabets = {
'./Images/gir.gif':'binary',
'./Images/cernettes.jpg':'variant1',
'./Images/gob.jpg':'variant2',
'./Images/beyonce-upgrade.jpg':'variant3',
'./Images/metropolis.jpg':'variant4',
'./Images/grendel.jpg':'blocks',
'./Images/zero-cool.jpg':'greyscale'
};
var result = [];
var images = fs.readdirSync('./Images');
images = images.filter(function(image){
return image[0] !== '.';
}).map(function(image){
return './Images/'+image;
});
var count = 0;
arrays.forAllEmissions(images, function(item, key, done){
var options = { filepath: item };
if(needsAccurateColor.indexOf(item) !== -1) options.distance = moreAccurateColor;
if(alternateAlphabets[item]) options.alphabet = alternateAlphabets[item];
var image = new art.Image(options);
var file = options.filepath.split('/').pop();
var pos = file.indexOf('.');
var name = (pos===-1?file:file.substring(0, pos));
var label = name.replace(/[_-]+/g, ' ').split(' ').map(function(str){
return str[0].toUpperCase()+str.substring(1);
}).join(" ");
var complete = done;
image.write(function(err, ascii){
if(err) console.log(err);
result[key] = ascii;
art.font(label, 'Doom', 'white', function(rendered){
result[key] += rendered;
if(process.argv[2] === 'save'){
fs.writeFile('./test/images/'+name+'.nfo', ascii, function(err){
count++;
complete();
});
}else{
complete();
}
});
});
}, function(){
if(process.argv[2] !== 'save') console.log(result.join("\n"));
art.image({
width : 40,
filepath : './Images/initech.png',
alphabet : 'wide'
}).font('INITECH', 'Doom', 'cyan', function(rendered){
if(process.argv[2] === 'save'){
fs.writeFile('./test/images/mixed.nfo', rendered);
count++;
}
if(process.argv[2] === 'save') console.log(count+' files saved.');
else console.log(rendered);
});
});