也许已经有一些工具可以执行此操作,但是在尝试使用时,您还可以使用一些屏幕截图工具和tesseract创建一个简单的脚本。
以这个脚本为例(在我的系统中,我将其另存为/usr/local/bin/screen_ts
):
#!/bin/bash
# Dependencies: tesseract-ocr imagemagick scrot
select tesseract_lang in eng rus equ ;do break;done
# Quick language menu, add more if you need other languages.
SCR_IMG=`mktemp`
trap "rm $SCR_IMG*" EXIT
scrot -s $SCR_IMG.png -q 100
# increase quality with option -q from default 75 to 100
# Typo "$SCR_IMG.png000" does not continue with same name.
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
并具有剪贴板支持:
#!/bin/bash
# Dependencies: tesseract-ocr imagemagick scrot xsel
select tesseract_lang in eng rus equ ;do break;done
# quick language menu, add more if you need other languages.
SCR_IMG=`mktemp`
trap "rm $SCR_IMG*" EXIT
scrot -s $SCR_IMG.png -q 100
# increase image quality with option -q from default 75 to 100
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
它用于scrot
获取屏幕,tesseract
识别文本并cat
显示结果。剪贴板版本还利用xsel
管道输出到剪贴板。
注:scrot
,xsel
,imagemagick
和tesseract-ocr
是不是默认安装的,但可从默认的存储库。
您可以更换scrot
同gnome-screenshot
,但它可能需要大量的工作。关于输出,您可以使用任何可以读取文本文件的内容(使用“文本编辑器”打开,将识别的文本显示为通知等)。
GUI版本的脚本
这是OCR脚本的简单图形版本,其中包括语言选择对话框:
#!/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 -q 100 #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 | xsel -bi # pass to clipboard
exit
除了上面列出的依赖项之外,您还需要从webupd8 PPA安装Zenity fork YAD才能使脚本正常工作。
gnome-screenshot -a
?还要为什么将输出通过管道传输到tesseract?如果我没看错,gnome屏幕截图会将图片保存在文件中,而不是“打印”它