forked from 3kh0/3kh0.github.io-replit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
img2txt.html
114 lines (114 loc) · 3.66 KB
/
img2txt.html
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
114
<!DOCTYPE html>
<html>
<link rel="icon" sizes="192x192" href="./img/img2txt.png" />
<head>
<title>img2txt | 3kh0</title>
<link href="./images/logo.png" rel="icon">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
max-width: 1000px;
margin: 20px auto;
padding: 0 20px;
font-family: sans-serif;
}
input,
button,
select,
textarea {
font: inherit;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
textarea {
width: 100%;
}
code {
white-space: pre;
letter-spacing: 0;
line-height: 1ch;
display: block;
}
label {
font-weight: bold;
padding-right: 10px;
}
</style>
</head>
<body>
<h1>Image to Scheme</h1>
<p><label>image:</label> <input type="file" id="input" accept="image/*" /><label>resolution (pixel square size to group):</label> <input type="number" id="resolution" value="3" /></p>
<h2>Output</h2>
<code id="output"></code>
<script>
function getPixelXY(imgData, x, y) {
// https://stackoverflow.com/questions/667045/getpixel-from-html-canvas
var i = (y * imgData.width + x) * 4,
d = imgData.data;
return [d[i], d[i + 1], d[i + 2], d[i + 3]];
}
function darkness(r, g, b) {
return Math.round((parseInt(r) * 299 + parseInt(g) * 587 + parseInt(b) * 114) / 1000);
}
function avg(arr) {
return arr.reduce((a, b) => a + b) / arr.length;
}
var input = document.querySelector("#input"),
resinput = document.querySelector("#resolution"),
output = document.querySelector("#output"),
canvas = document.createElement("canvas"),
c = canvas.getContext("2d"),
RESOLUTION;
input.onchange = (e) => {
RESOLUTION = +resinput.value;
var image = new Image(),
t = window.URL.createObjectURL(input.files[0]);
image.onload = (e) => {
(canvas.width = image.width), (canvas.height = image.height);
c.drawImage(image, 0, 0, image.width, image.height);
window.URL.revokeObjectURL(t);
var data = c.getImageData(0, 0, canvas.width, canvas.height),
newpixels = [];
for (var y = 0; y < canvas.height; y += RESOLUTION) {
var pixelrow = [];
for (var x = 0; x < canvas.width; x += RESOLUTION) {
var darknesses = [];
for (var i = 0, l = RESOLUTION * RESOLUTION; i < l; i++) {
var t = getPixelXY(data, x + (i % RESOLUTION), y + Math.floor(i / RESOLUTION));
if (t[0] !== undefined) darknesses.push(darkness(...t));
}
pixelrow.push(Math.round(avg(darknesses)));
}
newpixels.push(pixelrow);
}
output.innerHTML = newpixels
.map((a) =>
a
.map((a) => {
if (a < 25) return "@";
else if (a < 50) return "%";
else if (a < 75) return "#";
else if (a < 100) return "*";
else if (a < 125) return "+";
else if (a < 150) return "=";
else if (a < 175) return "-";
else if (a < 200) return ":";
else if (a < 225) return ".";
else return " ";
})
.join("")
)
.join("\n");
};
image.src = t;
};
</script>
<br />
</body>
<!-- made by 3kh0 -->
</html>