Skip to content

Instantly share code, notes, and snippets.

@didmar
Last active May 11, 2024 14:26
Show Gist options
  • Save didmar/4e0234f313c13f68ac3bc760fef5e3e0 to your computer and use it in GitHub Desktop.
Save didmar/4e0234f313c13f68ac3bc760fef5e3e0 to your computer and use it in GitHub Desktop.
Shell script to extract text from last screenshot taken
#!/bin/sh
# This script opens the last screenshot taken and runs OCR on it to extract text.
# It used tesseract (see https://tesseract-ocr.github.io/)
# Configured with Ubuntu's Screenshots folder. Change to your own.
SCREENSHOTS_DIR=$HOME/Pictures/Screenshots
# Create the folder to put the output into, if needed
mkdir -p $SCREENSHOTS_DIR/ocr_output
# Get the last screenshot taken
LAST_FILE="$SCREENSHOTS_DIR/$(ls --color=never -t $SCREENSHOTS_DIR | grep ".png" | head -n 1)"
# Run OCR on the screenshot
OUTPUT_FILE_WITHOUT_EXT="$SCREENSHOTS_DIR/ocr_output"
OUTPUT_FILE="${OUTPUT_FILE_WITHOUT_EXT}.txt"
tesseract "$LAST_FILE" "$OUTPUT_FILE_WITHOUT_EXT"
# Open the OCR output
echo ""
echo "==============================================="
echo "Extracted text from screenshot:"
echo "==============================================="
echo ""
cat "$OUTPUT_FILE"
# xdg-open "$OUTPUT_FILE"
# Optionally, copy text to clipboard (requires xclip to be installed)
# xclip -selection clipboard "$OUTPUT_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment