Skip to content

Instantly share code, notes, and snippets.

@CapMousse
Created July 20, 2024 20:07
Show Gist options
  • Save CapMousse/454ec041880e78b4e3a894e7a4033faa to your computer and use it in GitHub Desktop.
Save CapMousse/454ec041880e78b4e3a894e7a4033faa to your computer and use it in GitHub Desktop.
Screenshot OCR for text extraction on Hyprland
#!/bin/bash
# Dependencies: tesseract-ocr imagemagick wl-clipboard hyprshot
die(){
notify-send "$1"
exit 1
}
cleanup(){
[[ -n $1 ]] && rm -r "$1"
}
SCR_IMG=$(mktemp -d) || die "failed to create tmpdir"
# shellcheck disable=SC2064
trap "cleanup '$SCR_IMG'" EXIT
hyprshot -m region -f scr.png --silent -o $SCR_IMG || die "failed to take screenshot"
mogrify -modulate 100,0 -resize 400% "$SCR_IMG/scr.png" || die "failed to convert image"
tesseract "$SCR_IMG/scr.png" "$SCR_IMG/scr" &> /dev/null || die "failed to extract text"
wl-copy < "$SCR_IMG/scr.txt" || die "failed to copy text to clipboard"
notify-send "Text extracted from image" || die "failed to send notification"
exit
@rafaeloledo
Copy link

#!/usr/bin/env bash
# Dependencies: tesseract imagemagick wl-clipboard hyprshot

die() {
  notify-send "$1"
  exit 1
}
cleanup() {
  [[ -n $1 ]] && rm -r "$1"
}

SCR_IMG=$(mktemp -d) || die "failed to create tmpdir"

# shellcheck disable=SC2064
trap "cleanup '$SCR_IMG'" EXIT

hyprshot -m region -f scr.png --silent -o $SCR_IMG
sleep 0.1
mogrify -modulate 100,0 -resize 400% "$SCR_IMG/scr.png" || die "failed to convert image"
tesseract "$SCR_IMG/scr.png" "$SCR_IMG/scr" &>/dev/null || die "failed to extract text"
wl-copy <"$SCR_IMG/scr.txt" || die "failed to copy text to clipboard"
dunstify "Text extracted from image" || die "failed to send notification"
exit

Better shbang and changed tesseract-ocr to tesseract to arch and nixos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment