3

When I was still using Windows I loved using the capture2text OCR program to grab Japanese kanji from manga and dump them into jisho.org, and was wondering how I could get the same functionality on Ubuntu. Namely:

  1. Take a partial screenshot upon hitting a designated hotkey (click+drag style).
  2. Process the image through an OCR engine.
  3. Output the result into the clipboard.
Eliah Kagan
  • 117,780

1 Answers1

4

Based off of this script (the 2nd one) I whittled the script down to this:

#!/bin/bash 
# Dependencies: tesseract-ocr imagemagick gnome-screenshot xclip

SCR="/home/takingitcasual/Documents/Translate/temp"

gnome-screenshot -a -f $SCR.png

mogrify -modulate 100,0 -resize 400% $SCR.png 
#should increase detection rate

tesseract $SCR.png $SCR -psm 10 -l jpn
cat $SCR.txt | xclip -selection clipboard
#you can only scan one character at a time

exit

Some goals I had with the modified file:

  1. Removing the need for sudo (to allow for easy hotkey binding)
  2. Replacing scrot (the way gnome-screenshot works looks much nicer IMO)
  3. Simplify the script to something I can more easily understand (removal of temp files)
  4. Limit recognition to one character at a time. (recognition was abysmal without that "-psm 10", and Tesseract kept throwing "empty page" errors as well)

Another two things I did were:

chmod a+x /home/takingitcasual/Documents/Translate/

and setting the temp files permissions to read/write for all. Not sure if it was redundant to do both.

Last thing was giving the bash command

bash /home/takingitcasual/Documents/Translate/screen_ts.sh

a shortcut via this.

If anybody has suggestions on modifying the script or anything else I'd love to hear it. (I have no previous experience with scripting, so I'm sure it can be improved.)