Created
April 14, 2023 15:45
-
-
Save kentcdodds/9d49b047079acb3a2e133f7a55fd1837 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) { | |
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