Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created April 14, 2023 15:45
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/9d49b047079acb3a2e133f7a55fd1837 to your computer and use it in GitHub Desktop.
Save kentcdodds/9d49b047079acb3a2e133f7a55fd1837 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) {
const {data} = await Tesseract.recognize(clipboardImage, 'eng', {
logger: m => console.log(m),
})
clipboard.writeText(data.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) {
const {data} = await Tesseract.recognize(filePath, 'eng', {
logger: m => console.log(m),
})
clipboard.writeText(data.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