def script_ocrNavigatorObject(self, gesture):
nav = api.getNavigatorObject()
left, top, width, height = nav.location
img = ImageGrab.grab(bbox=(left, top, left + width, top + height))
# Tesseract copes better if we convert to black and white...
img = img.convert(mode='L')
# and increase the size.
img = img.resize((width * IMAGE_RESIZE_FACTOR, height * IMAGE_RESIZE_FACTOR), Image.BICUBIC)
baseFile = os.path.join(tempfile.gettempdir(), "nvda_ocr")
try:
imgFile = baseFile + ".bmp"
img.save(imgFile)
ui.message(_("Running OCR"))
lang = getConfig()['language']
# Hide the Tesseract window.
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESHOWWINDOW
si.wShowWindow = subprocess.SW_HIDE
subprocess.check_call((TESSERACT_EXE, imgFile, baseFile, "-l", lang, "hocr"),
startupinfo=si)
finally:
try:
os.remove(imgFile)
except OSError:
pass
try:
hocrFile = baseFile + ".html"
parser = HocrParser(file(hocrFile).read(),
left, top)
finally:
try:
os.remove(hocrFile)
except OSError:
pass
# Let the user review the OCR output.
nav.makeTextInfo = lambda position: OcrTextInfo(nav, position, parser)
api.setReviewPosition(nav.makeTextInfo(textInfos.POSITION_FIRST))
ui.message(_("Done"))
评论列表
文章目录