1. 下載第三方模塊
# 發送瀏覽器請求 pip install requests # 文字識別 pip install pytesseract # 圖片處理 pip install Pillow
2. (1)Pillow 中的 Image

# 注意:print_function的導入必須在Image之前,否則會報錯 from __future__ import print_function from PIL import Image """ pillow 模塊 中 Image 的基本使用 """ # 1.打開圖片 im = Image.open("../wordsDistinguish/test1.jpg") print(im) # 2.查看圖片文件內容 print("圖片文件格式:"+im.format)print("圖片大小:"+str(im.size)) print("圖片模式:"+im.mode) # 3.顯示當前圖片對象 im.show() # 4.修改圖片大小,格式,保存 size = (50, 50) im.thumbnail(size) im.save("1.jpg", "PNG") # 5.圖片模式轉化並保存,L 表示灰度 RGB 表示彩色 im = im.convert("L") im.save("test1.jpg")
(2)基於 Tesseract-OCR 的 pytesseract
Python-tesseract是python的光學字符識別(OCR)工具。也就是說,它將識別並“讀取”嵌入圖像中的文本。
Python-tesseract是Google的Tesseract-OCR引擎的包裝器。它作為獨立的調用腳本也很有用,因為它可以讀取Pillow和Leptonica成像庫支持的所有圖像類型,包括jpeg,png,gif,bmp,tiff等。此外,如果用作腳本,Python-tesseract將打印已識別的文本,而不是將其寫入文件。
Windows下安裝的話直接下載包即可,然后把其加入系統環境變量(即加入Path里)

# 從 Pillow 中導入圖片處理模塊 Image from PIL import Image # 導入基於 Tesseract 的文字識別模塊 pytesseract import pytesseract """ @pytesseract:https://github.com/madmaze/pytesseract """ # 打開圖片 im = Image.open("../wordsDistinguish/Resources/1.jpg") # 識別圖片內容 text = pytesseract.image_to_string(im) print(text)
3. 安裝 tesseract
https://github.com/UB-Mannheim/tesseract/wiki
4. 報出異常
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path
解決
5. 接着重新加載代碼
附屬鏈接https://blog.csdn.net/weixin_42232219/article/details/100048086