在 Python 中迭代图像并转换为 table OCR 的 base64 格式

Iterate images and convert to base64 format for table OCR in Python

我有几个来自 here 的图像格式表,我可以使用以下代码将它们一一提取。

import base64
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException 
from tencentcloud.ocr.v20181119 import ocr_client, models 

path = './test/1.png'
def imgget(path):
    with open(path,'rb') as f: 
        base64_data = base64.b64encode(f.read())
    return base64_data.decode('utf-8')

try: 
    cred = credential.Credential('***', '***') 
    httpProfile = HttpProfile()
    httpProfile.endpoint = 'ocr.tencentcloudapi.com'
 
    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = ocr_client.OcrClient(cred, 'ap-guangzhou', clientProfile) 
 
    req = models.TableOCRRequest()
    
    params = imgget(path)
    req.ImageBase64 = str(params)
    # req.from_json_string(params)
 
    resp = client.TableOCR(req)
    # print(resp.to_json_string())
 
except TencentCloudSDKException as err: 
    print(err)
    
data = base64.b64decode(resp.Data)

def save(data, name):
    path = name
    with open(path, 'wb') as f:
        f.write(data)
    f.close
# print(data)
name = './data/1.xlsx'
save(data, name)

但是现在我想迭代这个问题开头的 link 中的所有图像文件并将它们传递给 path = './test/1.png',然后提取表格并将它们保存在一个 sheet 中Python 中的一个 excel 文件,我如何修改上面的代码来做到这一点?

谢谢。

更新:

我们可以循环所有图像的路径:

for path in glob.iglob('./test/*.png'):
    print(path)

输出:

./test/4.png
./test/3.png
./test/2.png
./test/1.png

我目前的解决方案,无法将结果保存到一个 excel 文件中,但它以某种方式工作。

欢迎改进

import base64
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException 
from tencentcloud.ocr.v20181119 import ocr_client, models 
import glob
import os

directory = './images/test/'

def imgget(path):
    with open(path,'rb') as f: 
        base64_data = base64.b64encode(f.read())
    return base64_data.decode('utf-8')

# save files
def save(data, name):
    path = name
    with open(path, 'wb') as f:
        f.write(data)
    f.close
                
try: 
    cred = credential.Credential('***', '***') 
    httpProfile = HttpProfile()
    httpProfile.endpoint = 'ocr.tencentcloudapi.com'
 
    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = ocr_client.OcrClient(cred, 'ap-guangzhou', clientProfile) 
 
    req = models.TableOCRRequest()
    
    for image in os.listdir(directory):        
        if image.endswith(".png") or image.endswith(".jpg"):
            print(image)
            file_names = os.path.splitext(image)[0]
            print(file_names)
            path = os.path.join(directory, image)
            params = imgget(path)
            req.ImageBase64 = str(params)
            # req.from_json_string(params)
            resp = client.TableOCR(req)

            data = base64.b64decode(resp.Data)
            print(data)
            name = './tables/{}.xlsx'.format(file_names)
            save(data, name)
            continue
        else:
            pass
 
except TencentCloudSDKException as err:
    print(err)