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
/*
Copyright (c) 2018 Victor Ribeiro - victorqribeiro@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
https://github.com/victorqribeiro/imgToAscii
*/
// Converted from: https://github.com/victorqribeiro/imgToAscii/blob/ca7e181b9bb9770798ed3a0d3dfeb344c60953f2/src/imgToAscii.js
import { createCanvas, loadImage } from "https://deno.land/x/canvas@v1.4.1/mod.ts";
export async function imageToAscii(src: string, maxWidth?: number) {
let string = "";
let stringColor = "";
const charType = 1;
// dprint-ignore
const alphabet = {
0: ["@","%","#","*","+","=","-",":","."," "],
1: ["$","@","B","%","8","&","W","M","#","*","o","a","h","k","b","d","p","q","w","m","Z","O",
"0","Q","L","C","J","U","Y","X","z","c","v","u","n","x","r","j","f","t","/","\\","|","(",
")","1","{","}","[","]","?","-","_","+","~","\<","\>","i","!","l","I",";",":",",","\"","^",
"`","'","."," "]
}
const image = await loadImage(src);
let width = image.width();
const scale = maxWidth ? maxWidth / width : 1;
const height = Math.floor(image.height() * scale * 0.6);
width = Math.floor(width * scale);
const canvas = createCanvas(width, height);
const context = canvas.getContext("2d");
context.drawImage(image, 0, 0, canvas.width, canvas.height);
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
let grayStep = Math.ceil(255 / alphabet[charType].length);
for (let i = 0; i < imageData.data.length; i += 4) {
for (let j = 0; j < alphabet[charType].length; j++) {
let r = imageData.data[i];
let g = imageData.data[i + 1];
let b = imageData.data[i + 2];
if ((r * 0.2126) + (g * 0.7152) + (b * 0.0722) < (j + 1) * grayStep) {
const char = alphabet[charType][j];
string += char;
stringColor += "<span style=\"color: rgb(" + r + "," + g + "," + b + "); \">"
+ (char.replace(" ", "&nbsp;"))
+ "</span>";
break;
}
}
if (!((i / 4 + 1) % canvas.width)) {
string += "\n";
stringColor += "<br>";
}
}
return { string, stringColor };
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v33
June 9, 2024