-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathimage.js
48 lines (44 loc) · 1.08 KB
/
image.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
// @ts-check
import Jimp from "jimp";
import _imageToAscii from 'image-to-ascii'
import tempy from 'tempy';
import { promisify } from 'util'
const imageToAscii = promisify(_imageToAscii)
/**
* @param {number} time
*/
async function delay (time) {
return new Promise(resolve => setTimeout(resolve, time))
}
/**
* Takes an image and converts it to ASCII art.
* @param {string} file
* @param {{ pixels?: string, width?: number, contrast?: number }} [options]
* @returns {Promise<string>}
*/
export async function transform (file, {
pixels = "7772299408",
width = 32,
contrast = 0.1
} = {}) {
const im = await Jimp.read(file)
const temp = tempy.file({ extension: 'png' })
await im.contrast(contrast).write(temp);
await delay(2000)
const result = await imageToAscii(temp, {
size: {
width
},
size_options: {
px_size: {
width: 2,
height: 1
}
},
colored: false,
// Light to Dark
pixels,
reverse: true,
});
return result
}