Krolyxon
Commit c531183

Branches

Refs →

aurora.localbinocr

25 lines · 910 B · Bourne Again Shell (bash)

1#!/bin/sh
2# OCR a screen region: slurp -> grim -> tesseract -> clipboard, with a notification showing what was read.
3# usage: ocr [lang] (tesseract language, default eng; e.g. `ocr eng+hin`)
4# slurp takes aurora's matugen colours when they're there.
5
6lang="${1:-eng}"
7c="${XDG_CACHE_HOME:-$HOME/.cache}/matugen/aurora.json"
8
9if command -v jq >/dev/null && [ -r "$c" ]; then
10 bg=$(jq -r '.surfaceLowest // empty' "$c")
11 fg=$(jq -r '.primary // empty' "$c")
12fi
13if [ -n "$bg" ] && [ -n "$fg" ]; then
14 g=$(slurp -b "${bg}88" -c "${fg}ff" -s "#00000000" -w 1) || exit 0
15else
16 g=$(slurp) || exit 0
17fi
18
19text=$(grim -g "$g" - | tesseract stdin stdout -l "$lang" 2>/dev/null | sed -e 's/[[:space:]]*$//' -e '/./,$!d')
20if [ -z "$text" ]; then
21 notify-send -a ocr "OCR" "no text found"
22 exit 1
23fi
24printf '%s' "$text" | wl-copy
25notify-send -a ocr "OCR · copied" "$(printf '%s' "$text" | head -c 200)"