Monday, May 30, 2016

script to do ocr on screenshot

script to do ocr (optical character recognition) from screenshot

1- install some necessary tools
$ sudo apt-get install scrot
$ sudo apt-get install tesseract*

2- save the script below with file name

import sys
import os
import subprocess

os.system("scrot 1.jpg -s")
if len(sys.argv) == 1:
 os.system("tesseract 1.jpg 1")
 langs = subprocess.Popen(['tesseract','--list-langs'],stderr=subprocess.PIPE)
 llangs ="\n")
 lang = sys.argv[1]
 meet = False
 for i in llangs:
  if lang == i.strip():
   meet = True
 if meet == False:
  print "your input language does not exist"
  for i in llangs:
   print i
 os.system("tesseract -l " + lang + " 1.jpg 1")

os.system("cat 1.txt")

3- usages
- if there's no any argument, the default langauge is English
$ python
then you select the screen portion that you want to to the ocr

- if you want to do ocr on other language, provide the language code the the argument.
$ python chi_sim

screenshot of ocr of english language and simplified chinese language

No comments:

Post a Comment