-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (38 loc) · 1.11 KB
/
index.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
// Imports / Packages
const jimp = require("jimp"),
fs = require("fs");
// Const
const GREYSCALE =
" .'`^\",:;Il!i><~+_-?][}{1)(|/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$";
const INPUT_FILE = "image.png";
const OUTPUT_FILE = "output.txt";
// ASCII output variable
let finalString = "";
jimp.read(INPUT_FILE, (err, image) => {
if (err) throw err;
// Resize for large images
if (image.bitmap.width > 500 && image.bitmap.height > 500) {
image.resize(500, 500);
}
// Greyscale the image
image.greyscale();
// Loop each pixel
for (let y = 0; y < image.bitmap.height; y++) {
for (let x = 0; x < image.bitmap.width; x++) {
// Get ASCII letter based on the pixel color
finalString +=
GREYSCALE[
Math.floor(
(GREYSCALE.length - 1) *
(jimp.intToRGBA(image.getPixelColor(x, y)).r / 255).toFixed(2)
)
];
}
finalString += "\n";
}
// Write file with the output text
fs.writeFileSync(OUTPUT_FILE, finalString, (err) => {
if (err) throw err;
});
console.log("Saved");
});