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.
3

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.)

4
  • I have a similar question related to this question. Can you please check it out askubuntu.com/questions/1038099/… – blueray May 19 '18 at 14:12
  • --psm 10 is useful if the image has a single character. – Dominic108 Apr 28 '20 at 15:54
  • -l jpn is useful if the text is in Japanese. – Dominic108 Apr 28 '20 at 15:54
  • -psm is a typo. It must be --psm – Dominic108 Apr 28 '20 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.