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 screenshot-ocr.py

import sys
import os
import subprocess

os.system("scrot 1.jpg -s")
if len(sys.argv) == 1:
 os.system("tesseract 1.jpg 1")
else:
 langs = subprocess.Popen(['tesseract','--list-langs'],stderr=subprocess.PIPE)
 llangs = langs.stderr.read().split("\n")
 lang = sys.argv[1]
 meet = False
 for i in llangs:
  if lang == i.strip():
   meet = True
   break
 if meet == False:
  print "your input language does not exist"
  for i in llangs:
   print i
  sys.exit()
 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 screenshot-ocr.py
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 screenshot-ocr.py chi_sim

screenshot of ocr of english language and simplified chinese language



No comments:

Post a Comment