合并图片并利用百度API一次性OCR识别

本文介绍了如何使用Python进行批量图片处理,包括图片的提取、尺寸检测、图片合并,以及通过百度API实现合并图片的一次性OCR识别,提取出图片中的文字信息。
摘要由CSDN通过智能技术生成

1.批量提取图片

import requests
import re
from fontTools.ttLib import TTFont
from lxml import etree
from aip import AipOcr  #pip install baidu-aip
def readme(str,i):   #记事本,读取内容
    f=open(str,'r+')
    lines=f.readlines()
    lines[i]=lines[i].strip('\r\n')
    return lines[i]
for i in range(0,6):
#https://vip.gxrc.com/Public/Phone/4354E140-AF4E-4D46-B5C0-04AAD3A81F2F
#https://vip.gxrc.com/Public/Phone/113F1C6B-56E2-44DF-93A0-178E25ACC62E
#https://vip.gxrc.com/Public/Phone/FB4F6C7D-E4F9-442B-B8B8-603993782DAD
#https://vip.gxrc.com/Public/Phone/57EE4AF3-63D4-490A-A75C-98D4E5AFA5D8
#https://vip.gxrc.com/Public/Phone/266E2D9B-2ED9-4ED2-BE8A-565A8C91F5CD
#https://vip.gxrc.com/Public/Phone/6C73242B-DE0C-490B-A715-3DF7BCED7825
    r=requests.get(readme('k:/zhusc/src_url.txt',i))
    with open('k:/zhusc/ps/{}.png'.format(str(i)),'wb') as f:#一定要png,jpg无效
        f.write(r.content)

以下代码绝大部分来自【kimol君的无聊小发明】—用python写图片格式批量处理工具
https://blog.csdn.net/kimol_justdo/article/details/111239805?utm_source=app&app_version=4.5.0
2.批量测试图片的宽度、高度

from PIL import Image
from pathlib import Path
import os
from shutil import copyfile

def resize(inImage, width, height, inplace=False):
    '''
    将图片调整为指定尺寸
    ----------------------------
    参数 inImage:需要处理的图片地址
    参数   width:预期图片宽度
    参数  height:预期图片高度
    参数 inplace:是否覆盖原文件
    ----------------------------
    返回 outImage:压缩后的图片地址
    '''
    if not inplace: # 如果不覆盖
        outImage = '%s-out.%s'%(inImage.split('.')[
好的,以下是制作一个调用百度APIOCR识别页面的步骤: 1. 首先,你需要注册百度开发者账号并创建一个 OCR 应用,获取 API Key 和 Secret Key。具体操作可以参考百度官方文档。 2. 在 HTML 页面中添加一个表单,用于上传图片。代码如下: ``` <form enctype="multipart/form-data" method="post" action=""> <input type="file" name="image"> <input type="submit" value="识别"> </form> ``` 3. 在 JavaScript 中使用 XMLHttpRequest 或 Fetch API 发送 POST 请求,将图片数据传输到百度 OCR API。代码如下: ``` const apiKey = 'Your API Key'; const secretKey = 'Your Secret Key'; const url = 'https://aip.baidubce.com/oauth/2.0/token'; const ocrUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic'; // 获取 access token fetch(`${url}?grant_type=client_credentials&client_id=${apiKey}&client_secret=${secretKey}`) .then(response => response.json()) .then(data => { const accessToken = data.access_token; // 读取图片数据并转换为 base64 编码 const fileInput = document.querySelector('input[type="file"]'); const file = fileInput.files[0]; const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => { const base64Data = reader.result.split(',')[1]; // 发送 OCR 请求 fetch(`${ocrUrl}?access_token=${accessToken}`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: `image=${encodeURIComponent(base64Data)}` }) .then(response => response.json()) .then(data => { console.log(data.words_result); // 在页面中显示识别结果 }) .catch(error => console.error(error)); }; }) .catch(error => console.error(error)); ``` 4. 在识别结果返回后,在页面中显示识别结果。你可以使用 DOM 操作来实现。 以上就是制作一个调用百度APIOCR识别页面的步骤。需要注意的是,百度 OCR API 有使用限制,请确保你已经了解并遵守相关政策。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值