This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertImage.ts
52 lines (39 loc) · 1.53 KB
/
convertImage.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
import 'std/dotenv/load.ts'
import { OcrSdk } from '../../core/OcrSdk.ts'
import { prettifyXml } from '../../core/prettifyXml.ts'
import { basename, dirname, join } from 'std/path/mod.ts'
import type { ExportFormat, Language } from '../../core/types.ts'
type Options = {
applicationId: string
password: string
serviceUrl: string
exportFormats: ExportFormat[]
languages: Language[]
}
export async function convertImage(imagePath: string, options: Options) {
const { applicationId, password, serviceUrl, exportFormats, languages } = options
const imageFileName = basename(imagePath)
const dirName = dirname(imagePath)
const bareFileName = imageFileName.split('.')[0]
const image = await Deno.readFile(imagePath)
const ocrSdk = new OcrSdk(applicationId, password, serviceUrl)
const outputs = await ocrSdk.ocr(image, {
exportFormats,
languages,
})
for (const [ext, file] of Object.entries(outputs)) {
let text = new TextDecoder().decode(await file.arrayBuffer()).replaceAll('\r\n', '\n')
if (file.type === 'application/xml') {
const { load } = await import('cheerio')
const $ = load(text, { xml: true })
const $block = $('<block blockType="Picture" blockName="cl:original-image"><pictureFile /></block>')
$block.find('pictureFile').attr('path', imageFileName)
$block.insertBefore($('page').eq(0).find('block').eq(0))
text = prettifyXml($.xml())
}
const outPath = join(dirName, `${bareFileName}-result.${ext}`)
await Deno.writeTextFile(outPath, text)
console.info(`Wrote to ${outPath}`)
}
console.info('Done!')
}