[Python]圖像二值化


https://blog.csdn.net/qq_35531549/article/details/96134760

 

# 識別前處理
# 圖片二值化
from PIL import Image
import os
os.chdir('D:\OCR')
img = Image.open('test.png')

# 模式L”為灰色圖像,它的每個像素用8個bit表示,0表示黑,255表示白,其他數字表示不同的灰度。
Img = img.convert('L')
Img.save("test1.png")

# 自定義灰度界限,大於這個值為黑色,小於這個值為白色
threshold = 200

table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)

# 圖片二值化
photo = Img.point(table, '1')
photo.save("test2.png")
# 識別圖片內容
import pytesseract
img_path = 'test2.png'

text=pytesseract.image_to_string(Image.open(img_path))

print(text)

————————————————
版權聲明:本文為CSDN博主「寸草心2130」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_35531549/article/details/96134760


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM