Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created April 14, 2023 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kentcdodds/95331639fddae39c3a0aa2c651fbb498 to your computer and use it in GitHub Desktop.
Save kentcdodds/95331639fddae39c3a0aa2c651fbb498 to your computer and use it in GitHub Desktop.
// Menu: Optical Character Recognition
// Description: Extract text from images
// Author: Kent C. Dodds
// Twitter: @kentcdodds
import '@johnlindquist/kit'
import Tesseract from 'tesseract.js'
const clipboardImage = await clipboard.readImage()
if (clipboardImage.byteLength) {
Tesseract.recognize(clipboardImage, 'eng', {
logger: m => console.log(m),
}).then(({data: {text}}) => {
clipboard.writeText(text)
})
} else {
let selectedFiles = await getSelectedFile()
let filePaths: Array<string>
if (selectedFiles) {
filePaths = selectedFiles.split('\n')
} else {
let droppedFiles = await drop({placeholder: 'Drop images to compress'})
filePaths = droppedFiles.map(file => file.path)
}
for (const filePath of filePaths) {
Tesseract.recognize(filePath, 'eng', {logger: m => console.log(m)}).then(
({data: {text}}) => {
clipboard.writeText(text)
},
)
}
}
notify({
title: 'OCR finished',
message: `Copied text to your clipboard`,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment