TextSnatcher: Copy text from images, for the Linux Desktop
github.com
github.com
I didn't test the x11/wayland check yet, but feel free to use it and report back.
#!/bin/bash
# Dependencies: tesseract-ocr imagemagick
# on gnome: gnome-screenshot
# on kde: spectacle
# on x11: xsel
# on wayland: wl-clipboard
die(){
notify-send "$1"
exit 1
}
cleanup(){
[[ -n $1 ]] && rm -rf "$1"
}
SCR_IMG=$(mktemp) || die "failed to take screenshot"
# shellcheck disable=SC2064
trap "cleanup '$SCR_IMG'" EXIT
notify-send "Select the area of the text"
if which "spectacle" &> /dev/null
then
spectacle -r -o "$SCR_IMG.png" || die "failed to take screenshot"
else
gnome-screenshot -a -f "$SCR_IMG.png" || die "failed to take screenshot"
fi
# increase image quality with option -q from default 75 to 100
mogrify -modulate 100,0 -resize 400% "$SCR_IMG.png" || die "failed to convert image"
#should increase detection rate
tesseract "$SCR_IMG.png" "$SCR_IMG" &> /dev/null || die "failed to extract text"
if [ "$XDG_SESSION_TYPE" == "wayland" ]
then
wl-copy < "$SCR_IMG.txt" || die "failed to copy text to clipboard"
else
xsel -b -i < "$SCR_IMG.txt" || die "failed to copy text to clipboard"
fi
notify-send "Text extracted"
exit
edit:Formatting
#!/usr/bin/env bash
langs=(eng ara fas chi_sim chi_tra deu ell fin heb hun jpn kor nld rus tur)
lang=$(printf '%s\n' "${langs[@]}" | fuzzel -d "$@")
grim -g "$(slurp)" - | mogrify -modulate 100,0 -resize 400% png:- | tesseract -l eng+${lang} - - | wl-copy
notify-send "Text extracted"My version has more feedback for the user which was important because the user was somebody not familiar with linux/bash, but even my version "swallows" errors.
#!/usr/bin/env bash
langs=(eng ara fas chi_sim chi_tra deu ell fin heb hun jpn kor nld rus tur)
lang=$(printf '%s\n' "${langs[@]}" | dmenu "$@")
maim -us | tesseract --dpi 145 -l eng+${lang} - - | xsel -bi
[0]: https://news.ycombinator.com/item?id=33704483#33705272 # shellcheck disable=SC2064
trap "cleanup '$SCR_IMG'" EXIT
While shellcheck can have false positives, and SCR_IMG probably doesn't have any characters which need escaping, it's not exactly wrong in this case.The command passed to `trap` is evaluated normally, so variable expansions do take place.
trap 'cleanup "$SCR_IMG"' EXIT
Will behave correctly, and the expansion of SCR_IMG won't be susceptible to issues relating to unquoted shell characters.Alternatively, if you're using a modern bash (this probably won't work on a mac by default), then this is an option too:
trap "cleanup ${SCR_IMG@Q}" EXIT #!/bin/bash
# Dependencies: tesseract-ocr imagemagick
# on gnome: gnome-screenshot
# on kde: spectacle
# on x11: xsel
# on wayland: wl-clipboard
die(){
notify-send "$1"
exit 1
}
cleanup(){
[[ -n $1 ]] && rm -r "$1"
}
SCR_IMG=$(mktemp -d) || die "failed to take screenshot"
# shellcheck disable=SC2064
trap "cleanup '$SCR_IMG'" EXIT
#notify-send "Select the area of the text"
if which "spectacle" &> /dev/null
then
spectacle -b -r -o "$SCR_IMG/scr.png" || die "failed to take screenshot"
else
gnome-screenshot -a -f "$SCR_IMG/scr.png" || die "failed to take screenshot"
fi
# increase image quality with option -q from default 75 to 100
mogrify -modulate 100,0 -resize 400% "$SCR_IMG/scr.png" || die "failed to convert image"
#should increase detection rate
tesseract "$SCR_IMG/scr.png" "$SCR_IMG/scr" &> /dev/null || die "failed to extract text"
if [ "$XDG_SESSION_TYPE" == "wayland" ]
then
wl-copy < "$SCR_IMG/scr.txt" || die "failed to copy text to clipboard"
else
xsel -b -i < "$SCR_IMG/scr.txt" || die "failed to copy text to clipboard"
fi
notify-send "Text extracted"
exitWhy did you remove the -f parameter?
Also made some minor modifications: replaced `xsel` with `xclip` and added truncated version of the copied text to the `notify-send`:
#!/bin/bash
# Dependencies: tesseract-ocr imagemagick
# on gnome: gnome-screenshot
# on kde: spectacle
# on x11: xsel
# on wayland: wl-clipboard
die(){
notify-send "$1"
exit 1
}
cleanup(){
[[ -n $1 ]] && rm -r "$1"
}
SCR_IMG=$(mktemp -d) || die "failed to take screenshot"
# shellcheck disable=SC2064
trap "cleanup '$SCR_IMG'" EXIT
#notify-send "Select the area of the text"
if which "spectacle" &> /dev/null
then
spectacle -n -b -r -o "$SCR_IMG/scr.png" || die "failed to take screenshot"
else
gnome-screenshot -a -f "$SCR_IMG/scr.png" || die "failed to take screenshot"
fi
# increase image quality with option -q from default 75 to 100
mogrify -modulate 100,0 -resize 400% "$SCR_IMG/scr.png" || die "failed to convert image"
#should increase detection rate
tesseract "$SCR_IMG/scr.png" "$SCR_IMG/scr" &> /dev/null || die "failed to extract text"
if [ "$XDG_SESSION_TYPE" == "wayland" ]
then
wl-copy < "$SCR_IMG/scr.txt" || die "failed to copy text to clipboard"
else
# xsel -b -i < "$SCR_IMG/scr.txt" || die "failed to copy text to clipboard"
xclip -selection clipboard -i < "$SCR_IMG/scr.txt" || die "failed to copy text to clipboard"
fi
# Notify the user what was copied but truncate the text to 100 characters
notify-send "Text extracted from image" "$(head -c 100 "$SCR_IMG/scr.txt")" || die "failed to send notification"
exitFor anyone running into barriers, mate-screenshot has no outfile `-f` option, so I worked around that by outputting through clipboard and capturing that with `xclip` (note, this is earlier in the script than the the xsel/xclip line in the parent and gp comments):
mate-screenshot -a -c && xclip -selection clipboard -t image/png -o > "$SCR_IMG/scr.png" || die "failed to take screenshot"
The other hiccup is that the dumped text file has two extraneous bytes '\x0a\x0c', so I truncated them with `head`: (xclip -selection clipboard -i < <(head -c -2 "$SCR_IMG/scr.txt")) || die "failed to copy text to clipboard"
Might not be pretty, but it looks like this will work for me. Thank you all for this!I just press the hotkey (Super+O), drag the selection over whatever I want to OCR, then immediately get a popup dialog containing the captured text.
#!/bin/bash
# Dependencies: tesseract-ocr imagemagick scrot xsel
IMG=`mktemp`
trap "rm $IMG*" EXIT
scrot -s $IMG.png -q 100
# increase image quality with option -q from default 75 to 100
mogrify -modulate 100,0 -resize 400% $IMG.png
#should increase detection rate
tesseract $IMG.png $IMG &> /dev/null
cat $IMG.txt | xsel -bi
notify-send "Text copied" "$(cat $IMG.txt)"
exitI'd love to if TextSnatcher does anything to improve on this. The github page is opaque.
https://github.com/RajSolai/TextSnatcher/blob/master/src/ser...
The script calls Tesseract in default page segmentation mode (PSM 3). [1]
Depending on the input text, PSM mode 11 for disconnected text would probably work much better. That uses the flag "--psm 11".
[1] From the original repo: string tess_command = "tesseract " + file_path + " " + out_path + @" -l $lang" ;
Granted there's a lot of ambiguity in sans serif fonts, lower-case "L", vertical bar, and upper-case "i" can even be pixel-identical, but I've seen tesseract turn
Chapter III
into Chapter |l1
which really surprises me. In fact, for books, I run it through sed to replace vertical bar with upper-case "i" and it significantly improved recognition. maim -s --nodrag --quality=10 $IMG.png
10 is scrot's 100 import "$tempfile"
TEXT=`tesseract -l eng+deu "$tempfile" stdout`
echo "$TEXT" | xsel -i -bApologies to all of my fellow Unix-Windows borderers.
trap "rm $IMG*" EXIT
see https://www.shellcheck.net/wiki/SC2064also, use mktemp -d and recursively delete the directory
WIN+SHIFT+S
If it doesn't have the "Text actions" icon (dashed square with paragraph lines in it), you can update it via windows store to get the latest version.
I just really don't know, it feels like it's, uh, subpar. Isn't it? I never seriously worked in that domain, but it somehow felt to me in the 2019, that with all recent advancements in computer vision, text recognition must be essentially a solved problem. I'd expect it to be better than human. Yet I still cannot accurately convert a low-res scan (scan! not even a photo!) of a receipt with tesseract, especially if it isn't in English. Maybe I just cannot properly use it?
Myself I tried it probably 10-15 years ago on scanned scientific papers (decent scanning quality). The results were disappointing. The manual postprocessing required was not much less than typing it directly. So tesseract became a synonym of "not worth trying" to me.
Maybe things have improved over the years, so I should give it a new try. (No particular use case at the moment, but those tend to appear occasionally.)
Being a Flatpak app, it will require desktop portals to fully work. That said, it worked absolutely fine out of the box for me with my existing xdg-desktop-portal-wlr setup. So, it should work fine in any X11 or Wayland setup where you have an xdg-desktop-portal setup that supports the Screenshot API.
The results are mixed, but not bad by any means. Cleanly readable text comes out mostly fine with maybe only whitespace issues and the occasional error, which makes this still potentially very useful for copying text out of error dialogs and whatnot. (Though, I've found that on Linux, error dialogs are far more likely to have selectable text in the first place. And on Windows, standard MessageBox responds to Ctrl+C.)
I like the author.
- take screenshot
- extract text from it
- translate the text to english. Auto detects source language
- show both original and translated text in quick view where you can select and copy if desired.
Here is a implementation you can try:
https://www.icloud.com/shortcuts/f420d24e4960415da1a43f230ab...
While on the subject of iOS. In recent versions when you open a photo in the photos app you can also select the text in the photo by hand and copy it.
Caveat: This is a Flatpak and not all Linux distros ship with Flatpak. But I'll give it a whirl in my Fedora virtual machine. I've seen many flavors of this type of tool floating about, most of them leveraging Tesseract[0], and I've tried a few of them. It fails badly on grainy / noisy images or where the text is warped or skewed in some way. It will not solve CAPTCHAs for you!
Flatpak should work on every distro. However, it may not be included by default, so you need to install flatpak before installing this application.
Probably quite the overkill in terms of energy efficiency for just image to text, but I only need this like once every two weeks or so.
#!/bin/bash
# Performs Optical Character Recognition (OCR) on a freely chosen
# screen area and copies the recognized text to the clipboard.
#
# Dependencies: sudo apt install gnome-screenshot tesseract-ocr xclip
IMAGE_FILE="/tmp/ocr.png"
gnome-screenshot --area --file "$IMAGE_FILE"
tesseract "$IMAGE_FILE" - | xclip -rmlastnl -selection clipboard
rm -f "$IMAGE_FILE"Also code not being updated is something, TLS certificate not being renewed is in an other league in terms of lack of support of the project.
https://robbyzambito.me/posts/tips-and-tricks-for-taking-scr...
mkdir -p ~/Pictures/Screenshots
and not have to warn the user to create it.(would definitely appreciate feedback/critiques from any swifties out there)
[0]: https://github.com/RhetTbull/textinator
[1]: https://developer.apple.com/documentation/vision?language=ob...
[2]: https://support.apple.com/guide/preview/interact-with-text-i...
very nice though thanks for sharing!
If that's really the case then obviously don't use it for personal data (invoices, love letters, legal proceedings, ...)
https://github.com/RajSolai/TextSnatcher/blob/9e67760d6c16ea...
Tesseract itself seems to be included in the Flatpak as you'd expect:
https://github.com/RajSolai/TextSnatcher/blob/master/manifes...
Where did you get that?
which promises to grab text from an image and then translate and make new image with same style..
But I've tried it many times with different images this week and keep getting same error.
Could be that Canva has this and I've not aware of it yet.
I've needed this this week, but may just open up affinity designer and make one manually at this point.
If anyone has anything near a Linux equivalent, please let me know.
I take a photo, grab the text and send it via WhatsApp web app.
Not easy as a clip to clipboard but I haven’t found any on windows.
Source: helped a friend with the feature recently, he didn’t know it exists.
If you're on Android 14 you can also copy text through the recents/overview menu simply by highlighting the text. And finally there's Google Photos if you don't have any of these features.
There's also Google lens if you're trying to copy text that isn't on your screen.
Side note, what a breath of fresh air Gnome on Fedora is!
It's remarkable how much more polished Gnome is from a few years ago. If you use 2FA TOTP, make sure to install Gnome Authenticator if you haven't already. If you use Aegis on Android (or a handful of other formats) it can import/export your seeds. It is downright luxurious having this on my laptop/desktop:
# If you haven't setup flathub yet
sudo flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
# Install Authenticator from flathub. Source: https://gitlab.gnome.org/World/Authenticator
sudo flatpak install flathub com.belmoussaoui.Authenticator