图片识别文字

图片识别文字_第1张图片

安装包:

pip install baidu-aip


from aip import AipOcr
from PIL import Image

path='压力报告.png'

###改变图片尺寸

def ResizeImage(path2):
    filein = path2
    fileout = path2
    width = 2000
    height = 2500
    img = Image.open(filein)
    out = img.resize((width, height),Image.ANTIALIAS)
    out.save(fileout)
    img.close()
    
###读取图片指标
def get_file_content(filepath):

    ResizeImage(path)

    APP_ID = '24414511'
    API_KEY = 'OUogI3CydVfG54yeK4NwnYQt'
    SECRET_KEY = 'swhZn760fvuTAvppUHbCC3CAkRK7Xngw'
    
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    with open(filepath, 'rb') as fp:
        image = fp.read()
    fp.close()
     # 定义参数变量
    options = {
        # 定义图像方向
        'detect-direction': 'true',
        'language-type': 'CHN_ENG'
    }
    result = client.general(image, options)

    for word in result['words_result']:
        print(word['words'])


get_file_content(path)

你可能感兴趣的:(python,计算机视觉,开发语言)