一定要将图片二值化,转化图像为白底黑字
案例代码python
import pytesseract
from PIL import Image
def read_img_ocr(img_path, standard=205):
"""
读取图片中文字内容
:param img_path:
:return:
"""
img = Image.open(img_path)
# 在将图片灰度转换,二值化
img = img.convert('L')
pixels = img.load()
for x in range(img.width):
for y in range(img.height):
if pixels[x, y] > standard:
pixels[x, y] = 255
else:
pixels[x, y] = 0
# 图像识别
result = pytesseract.image_to_string(img, config=r'--oem 3 --psm 6 -l chi_sim+eng')
lines = result.split()
return lines
if __name__ == '__main__':
res = read_img_ocr('../target_img/image-017.png')
print(res)
通常来说,白底黑字的图像在 OCR方面更容易获得更好的准确性,这是因为黑色字体在白色背景上形成更强的对比度,有利于文字的分割和识别。