Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

In Ubuntu 12.10, if I type

gnome-screenshot -a | tesseract output

it returns:

** Message: Unable to use GNOME Shell's builtin screenshot interface, resorting to fallback X11.

How can I select a text from the screen and convert it to text (clipboard or document)?

Thank you!

share|improve this question
2  
+1, this is a very interesting question! –  Glutanimate Apr 11 '13 at 22:33
    
You get that error using only gnome-screenshot -a? Also why you pipe the output to tesseract? If i'm not wrong gnome-screenshot saves the picture on a file, and does not "print" it... –  Salem Apr 12 '13 at 14:04
    
Warning should be harmless if I look through bugzilla. Question: what is the auto-save-directory? And did it drop anything in there? Interesting link: forums.debian.net/viewtopic.php?f=6&t=85683 –  Rinzwind Apr 12 '13 at 14:04
    
gnome-screenchot -a -c is supposed to copy selection to clipboard,isnt it?. but piping it to tesseract gives the same error. the default directory is home/pictures (works well). –  Erling Apr 12 '13 at 14:39
1  
Just done this using gnome-screenshot - I then had to edit the files to decrease the colour depth from 16m to 2 (it was black text on white background, but with today's fancy font smoothing and so-on, it wasn't really black) I then had to scale the image up to 200% of the original before I got an accurate OCR from tesseract - but it worked really well once I'd done that. –  user192591 Sep 13 '13 at 11:01

1 Answer 1

up vote 10 down vote accepted

Maybe there is already some tool that does that, but you can also create a simple script with some screenshot tool and tesseract, as you are trying to use.

Take as an example this script (in my system I saved it as /usr/local/bin/screen_ts):

#!/bin/bash
# Dependencies: tesseract-ocr imagemagick scrot

SCR_IMG=`mktemp`
trap "rm $SCR_IMG*" EXIT

scrot -s $SCR_IMG.pngOOO
mogrify -modulate 100,0 -resize 400% $SCR_IMG.png #should increase detection rate
tesseract $SCR_IMG.png $SCR_IMG &> /dev/null
cat $SCR_IMG.txt

exit

And with clipboard support:

#!/bin/bash 
# Dependencies: tesseract-ocr imagemagick scrot xsel

SCR_IMG=`mktemp`
trap "rm $SCR_IMG*" EXIT

scrot -s $SCR_IMG.png
mogrify -modulate 100,0 -resize 400% $SCR_IMG.png #should increase detection rate
tesseract $SCR_IMG.png $SCR_IMG &> /dev/null
cat $SCR_IMG.txt | xsel -bi

exit

It uses scrot to take the screen, tesseract to recognize the text and cat to display the result. The clipboard version additionally utilizes xsel to pipe the output into the clipboard.

sample usage

NOTE: scrot, xsel, imagemagick and tesseract-ocr are not installed by default but are available from the the default repositories.

You may be able to replace scrot with gnome-screenshot, but it may take a lot of work. Regarding the output you can use anything that can read a text file (open with Text Editor, show the recognized text as a notification, etc).


GUI version of the script

Here's a simple graphical version of the OCR script including a language selection dialog:

#!/bin/bash
# DEPENDENCIES: tesseract-ocr imagemagick scrot yad
# AUTHOR:       Glutanimate 2013 (http://askubuntu.com/users/81372/)
# NAME:         ScreenOCR
# LICENSE:      GNU GPLv3
#
# BASED ON:     OCR script by Salem (http://askubuntu.com/a/280713/81372)

TITLE=ScreenOCR # set yad variables
ICON=gnome-screenshot

# - tesseract won't work if LC_ALL is unset so we set it here
# - you might want to delete or modify this line if you 
#   have a different locale:

export LC_ALL=en_US.UTF-8

# language selection dialog
LANG=$(yad \
    --width 300 --entry --title "$TITLE" \
    --image=$ICON \
    --window-icon=$ICON \
    --button="ok:0" --button="cancel:1" \
    --text "Select language:" \
    --entry-text \
    "eng" "ita" "deu")

# - You can modify the list of available languages by editing the line above
# - Make sure to use the same ISO codes tesseract does (man tesseract for details)
# - Languages will of course only work if you have installed their respective
#   language packs (https://code.google.com/p/tesseract-ocr/downloads/list)

RET=$? # check return status

if [ "$RET" = 252 ] || [ "$RET" = 1 ]  # WM-Close or "cancel"
  then
      exit
fi

echo "Language set to $LANG"

SCR_IMG=`mktemp` # create tempfile
trap "rm $SCR_IMG*" EXIT # make sure tempfiles get deleted afterwards

scrot -s $SCR_IMG.png #take screenshot of area
mogrify -modulate 100,0 -resize 400% $SCR_IMG.png # postprocess to prepare for OCR
tesseract -l $LANG $SCR_IMG.png $SCR_IMG # OCR in given language
cat $SCR_IMG.txt | xsel -bi # pass to clipboard
exit

Aside from the dependencies listed above you will need to install the Zenity fork YAD from the webupd8 PPA to make the script work.

share|improve this answer
    
works greate in terminal! thank you! I want to screencopy codetext from tutorials for testing. How to use scrot to clipboard? –  Erling Apr 12 '13 at 15:07
1  
what happens with the tempfiles? –  Erling Apr 12 '13 at 15:18
    
The tempfiles stay there until you reboot your computer. If that is a problem to you, you can just delete them at the end (rm $SCR_IMG.png $SCR_IMG.txt). –  Salem Apr 12 '13 at 15:27
1  
scrot by itself can't use the clipboard. But there are tools like xclip or xsel that may do what you need if it is to copy/paste text. –  Salem Apr 12 '13 at 15:28
    
thank you so much, salem. that will safe time. –  Erling Apr 12 '13 at 15:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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