-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_webscrape.ts
203 lines (158 loc) · 6.06 KB
/
run_webscrape.ts
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import fs from 'fs';
import {loadImage, createCanvas} from 'canvas';
import {chromium} from 'playwright';
async function main() {
// Function to convert image to black and white and map it to a binary array
async function convertToBlackAndWhite(imagePath: string, targetWidth: number, startIndex=0, threshold = 128) {
const image = await loadImage(imagePath);
const targetHeight = Math.floor(image.height * (targetWidth / image.width));
const canvas = createCanvas(targetWidth, targetHeight);
const ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0, targetWidth, targetHeight);
const imageData = ctx.getImageData(0, 0, targetWidth, targetHeight);
const binaryPixels = new Array(targetWidth * targetHeight).fill(0);
const gridWidth = 60;
const startRow = Math.floor(startIndex / gridWidth);
const startCol = startIndex % gridWidth;
for (let row = 0; row < targetHeight; row++) {
for (let col = 0; col < targetWidth; col++) {
const pixelIndex = row * targetWidth + col;
const gridRow = startRow + Math.floor(pixelIndex / gridWidth);
const gridCol = (startCol + col) % gridWidth;
const originalIndex = (row * targetWidth + col) * 4;
const grayscale = (imageData.data[originalIndex] + imageData.data[originalIndex + 1] + imageData.data[originalIndex + 2]) / 3;
binaryPixels[gridRow * gridWidth + gridCol] = grayscale < threshold ? 1 : 0; // Apply threshold
}
}
let trimmedBinaryPixels = binaryPixels.slice(startIndex);
return { binaryPixels: trimmedBinaryPixels, width: targetWidth, height: targetHeight };
}
function createBlackAndWhiteImage(binaryPixels, width, height, outputPath) {
const canvas = createCanvas(width, height);
const ctx = canvas.getContext('2d');
const imageData = ctx.createImageData(width, height);
for (let i = 0; i < binaryPixels.length; i++) {
const colorValue = binaryPixels[i] === 1 ? 0 : 255; // 0 for black, 255 for white
const index = i * 4;
imageData.data[index] = colorValue; // R
imageData.data[index + 1] = colorValue; // G
imageData.data[index + 2] = colorValue; // B
imageData.data[index + 3] = 255; // A (full opacity)
}
ctx.putImageData(imageData, 0, 0);
// Save the canvas as an image file
const buffer = canvas.toBuffer('image/png');
fs.writeFileSync(outputPath, buffer);
console.log(`Black-and-white image saved to ${outputPath}`);
}
let images = [
/*
{
name: './image5.jpg',
threshold: 200
},
*/
{
name: './image4.jpg',
threshold: 200
},
{
name: './image3.jpg',
threshold: 160
},
{
name: './image2.jpg',
threshold: 128
},
{
name: './image8.jpg',
threshold: 200
},
/**
{
name: './image9.jpg',
threshold: 180
},
{
name: './image7.jpg',
threshold: 200
},
*/
{
name: './image10.jpg',
threshold: 200
},
{
name: './image11.jpg',
threshold: 180
},
];
/**
*
{
name: './image.jpg',
threshold: 128
}
*/
let binaryPixelList: Array<any> = [];
let startIndex = 1440;
for ( let index = 0; index < images.length; index++) {
let image = images[index];
convertToBlackAndWhite(image.name, 60, 0, image.threshold ).then(({ binaryPixels, width, height, }) => {
createBlackAndWhiteImage(binaryPixels, width, height, `./render_${index}.jpeg`);
});
let { binaryPixels, width, height } = await convertToBlackAndWhite( image.name, 60, startIndex, image.threshold );
binaryPixelList.push(binaryPixels)
}
console.log(`Start: ${startIndex}\n End: ${binaryPixelList[0].length + startIndex}`);
let browser = await chromium.launch(
//{headless: false}
);
let context = await browser.newContext();
let page = await context.newPage();
// Set the viewport size a better view.
// You're prolly better off using ctrl + scroll to resize the page anyway.
/**
page.setViewportSize({
width: 1000,
height: 2000
});
*/
await page.goto('https://bitmap.alula.me/', {waitUntil: "networkidle"});
await page.evaluate( async (startIndex) => {
await new Promise(r => setTimeout(r, 3000));
// @ts-ignore
app.$V.children.client.goToCheckboxCallback(startIndex);
await new Promise(r => setTimeout(r, 3000));
}, startIndex);
let currentBinaryPixel = 0;
while (true) {
await new Promise(r => setTimeout(r, 5000));
currentBinaryPixel = currentBinaryPixel + 1;
if (currentBinaryPixel == binaryPixelList.length) currentBinaryPixel = 0;
let chosenBinaryPixel = binaryPixelList[currentBinaryPixel];
await page.evaluate( async data => {
try {
data.chosenBinaryPixel.forEach(async (pixel: number, index: number) => {
index += data.startIndex;
// @ts-ignore
let isChecked = app.$V.children.client.isChecked(index);
if (
pixel === 1 && !isChecked ||
pixel === 0 && isChecked
) {
// @ts-ignore
app.$V.children.client.toggle(index)
}
});
}
catch (error) {
console.log(error);
process.exit(1)
}
}, {chosenBinaryPixel, startIndex} )
}
await context.close();
await browser.close();
}
main();