-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
138 lines (123 loc) · 5.15 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PicMadeFromTexts</title>
</head>
<body>
<h2>Pic Made From Unicode Texts</h2>
<p id="status">OpenCV.js is loading...</p>
<div>
<label for="textWidth">Text width: </label><input id="textWidth" type="number" value=50>
<label for="fontSize">Font size: </label><input id="fontSize" type="number" value=5>
<input type="checkbox" id="notSpace" name="notSpace" value="notSpace">
<label for="notSpace"> For FB/Discord/YT?</label><br>
<button onclick="process()">Process</button>
<div>
<img id="imageSrc" alt="No Image" />
<div>Input image: <input type="file" id="fileInput" name="file" /></div>
<textarea id="resultPic" name="resultPic" rows="50" cols="50" style="font-size: 5px;">
Output here
</textarea>
</div>
</div>
<script type="text/javascript">
const textWidthInput = document.getElementById("textWidth")
const fontSizeInput = document.getElementById("fontSize");
const imgElement = document.getElementById("imageSrc")
const inputElement = document.getElementById("fileInput");
const resultPic = document.getElementById("resultPic");
const notSpaceInput = document.getElementById("notSpace")
const optionsForm = document.getElementById("options")
inputElement.addEventListener("change", (e) => {
imgElement.src = URL.createObjectURL(e.target.files[0]);
}, false);
fontSizeInput.addEventListener("change", (e) => {
resultPic.style.fontSize = fontSizeInput.value + "px";
})
imgElement.onload = function () {
process();
}
function process() {
let textWidth = textWidthInput.value; // number of text characters in text pic
let src = cv.imread(imgElement);
const offset = 1.75
let result = convPicToTexts(src, textWidth, offset)
const srcHeight = src.size().height;
const srcWidth = src.size().width;
const rectWidth = Math.max(1, srcWidth / textWidth); // Length side of rect after division
//adaptive textarea
resultPic.rows = srcHeight / (offset * rectWidth);
resultPic.cols = srcWidth / rectWidth; // *2 cuz we add a space in between each char
console.log(resultPic.rows, resultPic.cols)
resultPic.value = result
}
function convPicToTexts(src, textWidth, offset) {
let result = "";
const srcHeight = src.size().height;
const srcWidth = src.size().width;
let graySrc = new cv.Mat();
cv.cvtColor(src, graySrc, cv.COLOR_RGBA2GRAY, 0);
let section = new cv.Mat();
const rectWidth = Math.max(1, srcWidth / textWidth); // Length side of rect after division
for (let j = 0; j < srcHeight; j += rectWidth * offset) { //top to bottom
for (let i = 0; i < srcWidth; i += rectWidth) { // left to right
let addWidth = rectWidth;
let addHeight = rectWidth * offset;
if (i + rectWidth > srcWidth) addWidth = srcWidth - i;
if (j + offset * rectWidth > srcHeight) addHeight = srcHeight - j;
let rect = new cv.Rect(i, j, addWidth, addHeight);
section = graySrc.roi(rect);
result += numToShade(averageArray(section.data));
section.delete()
}
result += '\n';
}
return result;
}
function onOpenCvReady() {
document.getElementById('status').innerHTML = 'OpenCV.js is ready.';
fetch("default.png").then(val => val.blob()).then(blob => { imgElement.src = URL.createObjectURL(blob);})
}
function averageArray(array) {
let total = 0;
for (let i = 0; i < array.length; i++) {
total += array[i];
}
return Math.round(total / array.length) || 0;
}
function numToShade(num) {
if (num >= 0 && num <= 50) {
return '█';
}
if (num >= 51 && num <= 101) {
return '▓';
}
if (num >= 102 && num <= 152) {
return '▒';
}
if (num >= 153 && num <= 203) {
return '░';
}
if (num >= 204 && num <= 256) {
return notSpaceInput.checked ? '┐' : ' ';
}
}
// function numToShade2(num) {
// if (num >= 0 && num <= 63) {
// return '█';
// }
// if (num >= 64 && num <= 127) {
// return '▓';
// }
// if (num >= 128 && num <= 191) {
// return '▒';
// }
// if (num >= 192 && num <= 256) {
// return '░';
// }
// }
</script>
<script async src="opencv.js" onload="onOpenCvReady();" type="text/javascript"></script>
</body>
</html>