Created
April 14, 2023 15:22
-
-
Save kentcdodds/cda0e651f383c115f40d25130450c889 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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