赞
踩
import fitz import time import re import os from PIL import Image from aip import AipOcr import numpy as np def pdf2pic(path, pic_path): ''' # 从pdf中提取图片 :param path: pdf的路径 :param pic_path: 图片保存的路径 :return: ''' t0 = time.perf_counter() # 使用正则表达式来查找图片 checkXO = r"/Type(?= */XObject)" checkIM = r"/Subtype(?= */Image)" # 打开pdf doc = fitz.open(path) # 图片计数 imgcount = 0 lenXREF = doc.xref_length() # 打印PDF的信息 print("文件名:{}, 页数: {}, 对象: {}".format(path, len(doc), lenXREF - 1)) # 遍历每一个对象 for i in range(1, lenXREF): # 定义对象字符串 text = doc.xref_object(i) isXObject = re.search(checkXO, text) # 使用正则表达式查看是否是图片 isImage = re.search(checkIM, text) # 如果不是对象也不是图片,则continue if not isXObject or not isImage: continue imgcount += 1 # 根据索引生成图像 pix = fitz.Pixmap(doc, i) # 根据pdf的路径生成图片的名称 new_name = f"{imgcount}.png" # 如果pix.n<5,可以直接存为PNG if pix.n < 5: pix.writePNG(os.path.join(pic_path, new_name)) # 否则先转换CMYK else: pix0 = fitz.Pixmap(fitz.csRGB, pix) pix0.writePNG(os.path.join(pic_path, new_name)) pix0 = None # 释放资源 pix = None t1 = time.perf_counter() print("运行时间:{}s".format(t1 - t0)) print("提取了{}张图片".format(imgcount)) class Tap_img: APP_ID = '' API_KEY = '' SECRET_KEY = '' def ResizeImage(self,img_path): '''改变图片尺寸''' filein = img_path fileout = img_path width = 500 height = 900 img = Image.open(filein) out = img.resize((width, height),Image.ANTIALIAS) out.save(fileout) img.close() def get_file_content(self,filepath): '''读取图片内容''' client = AipOcr(self.APP_ID, self.API_KEY, self.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) content_list=[] for word in result['words_result']: content_list.append(word['words']) res=''.join(content_list).split('%')[:-1] res_dict = {} for x in res: key=re.findall('[\u4e99-\u9fa5]+',x)[0] value=re.findall('\d+',x)[0] res_dict[key]=value+'%' return res_dict if __name__=='__main__': # 提取pdf的图片保存在本地 path = '1.pdf' pic_path = './img' pdf2pic(path, pic_path) ###将图片内容提取出来 img=Tap_img() content=img.get_file_content('img/2.png') print(content)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。