-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
img2txt.html
154 lines (140 loc) · 4.32 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8" />
<link href="./images/logo.png" rel="icon" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="./css/index.css">
<script src="https://cdn.jsdelivr.net/npm/@widgetbot/crate@3" async defer></script>
<title>img2txt | 3kh0</title>
<style>
.content {
max-width: 1000px;
margin: 20px auto;
padding: 0 20px;
}
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>
<div class="navbar">
<div class="navitems">
<img alt = "home" onclick="location.href='https://www.youtube.com/watch?v=dQw4w9WgXcQ'" src="./images/logo.png" class="navlogo"
draggable="false" />
<a href="./">
<div class="navitem">Home</div>
</a>
<a href="./projects">
<div class="navitem">Games</div>
</a>
<a href="./extras">
<div class="navitem">Extras</div>
</a>
<a href="./blog">
<div class="navitem">Blog</div>
</a>
<a href="./settings">
<div class="navitem">Settings</div>
</a>
<a href="./about">
<div class="navitem">About</div>
</a>
</div>
</div>
<div class="content">
<h1>Image to Text</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>
</div>
<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>
<script src="./js/index.js"></script>
<script src="./js/main.js"></script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
</body>
</html>