-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
image-processing.js
executable file
·34 lines (26 loc) · 1.14 KB
/
image-processing.js
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
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const { createWorker } = require('../..');
const [,, imagePath] = process.argv;
const image = path.resolve(__dirname, (imagePath || '../data/meditations.jpg'));
console.log(`Recognizing ${image}`);
// Tesseract.js returns images (imageColor, imageGrey, imageBinary) as strings
// to be used as source tags.
// This function converts to Uint8Array data for saving to disk.
const convertImage = (imageSrc) => {
const data = atob(imageSrc.split(',')[1])
.split('')
.map((c) => c.charCodeAt(0));
return new Uint8Array(data);
};
(async () => {
const worker = await createWorker();
const { data: { imageColor, imageGrey, imageBinary } } = await worker.recognize(image,
{ rotateAuto: true }, { imageColor: true, imageGrey: true, imageBinary: true });
console.log('Saving intermediate images: imageColor.png, imageGrey.png, imageBinary.png');
fs.writeFileSync('imageColor.png', convertImage(imageColor));
fs.writeFileSync('imageGrey.png', convertImage(imageGrey));
fs.writeFileSync('imageBinary.png', convertImage(imageBinary));
await worker.terminate();
})();