一、pytesseract
1.pytesseract是什么?
Pytesseract是一個Python的OCR庫,它可以識別圖片中的文本并將其轉(zhuǎn)換成文本形式。Pytesseract基于Google的Tesseract OCR引擎,具有較高的準確性和可靠性。它可以讀取多種格式的圖片,包括PNG、JPEG、GIF等。Pytesseract可以應用于自然語言處理、數(shù)據(jù)挖掘、OCR識別等領域。
2.安裝pytesseract
pip install pytesseract
3.查看pytesseract版本
pip show pytesseract
Name: pytesseract
Version: 0.3.10
Summary: Python-tesseract is a python wrapper for Google’s Tesseract-OCR
Home-page: https://github.com/madmaze/pytesseract
Author: Samuel Hoffstaetter
Author-email: samuel@hoffstaetter.com
License: Apache License 2.0
Requires: packaging, Pillow
Required-by:
4.安裝PIL
Pillow庫是Python圖像處理庫,pytesseract使用它來處理圖像。
pip install pillow
5.查看PIL版本
pip show pillow
Name: Pillow
Version: 9.4.0
Summary: Python Imaging Library (Fork)
Home-page: https://python-pillow.org
Author: Alex Clark (PIL Fork Author)
Author-email: aclark@python-pillow.org
License: HPND
Requires:
Required-by: image, imageio, matplotlib, pytesseract, wordcloud
二、Tesseract OCR
1.Tesseract OCR是什么?
Tesseract OCR是一種開源的OCR(Optical Character Recognition,光學字符識別)引擎,它能夠?qū)D像中的文本內(nèi)容識別并轉(zhuǎn)換為可編輯的文本格式。它最初由惠普實驗室開發(fā),現(xiàn)在由谷歌維護和更新。Tesseract OCR支持超過100種語言,包括中文、英文、法文、德文等。它可以在多種操作系統(tǒng)上運行,包括Windows、Linux、macOS等。Tesseract OCR被廣泛應用于數(shù)字化文檔、自動化數(shù)據(jù)輸入、智能搜索等方面。
2.安裝Tesseract OCR
macOS下:
brew install tesseract
3.安裝 Tesseract OCR 語言包
macOS下:
brew install tesseract-lang
三、使用方法
1.引入庫
import pytesseract
from PIL import Image
2.打開圖片文件
img = Image.open("demo.png")
3.使用Tesseract進行文字識別
text = pytesseract.image_to_string(img, lang='chi_sim')
4.輸出識別結(jié)果
print(text)
左:原圖
右:識別出的文字截圖
總結(jié)
image_to_string是一個Python函數(shù),它是由tesseract OCR引擎提供的。這個函數(shù)的作用是將一個圖像中的文本轉(zhuǎn)換成字符串,也就是把圖像中的文字識別出來,并把它們轉(zhuǎn)換成計算機可以處理的字符串格式。這個函數(shù)可以接受多種格式的圖像,例如JPEG、PNG、BMP等。在使用這個函數(shù)前,需要確保已經(jīng)安裝了tesseract OCR引擎。