Created
July 20, 2024 20:07
-
-
Save CapMousse/454ec041880e78b4e3a894e7a4033faa to your computer and use it in GitHub Desktop.
Screenshot OCR for text extraction on Hyprland
This file contains hidden or 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
#!/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 |
Works great, I changed from sleep 1 to sleep 0.1 to make it a bit faster. I also had to remove the || die ...
from the hyprshot.
Finally ended up with:
#!/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
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"
notify-send "Text extracted from image" || die "failed to send notification"
exit
@golgor I use an older version of hyprshot, maybe it’s related ?
@CapMousse, might be. I am using 1.3.0. It shouldn't return exit code 1, so might be a bug introduced in later version. It works fine with the minor changes. Thanks a lot for making this public :D
#!/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
And I spot a race condition where mogrify will mostly process the image before hyprshot even writes it. Causing weird errors like:
Adding a
sleep 1
works for me.