当前位置: 首页 > news >正文

改进拖放PDF转换为图片在转换为TXT文件的程序

前段时间我写了Python识别拖放的PDF文件再转成文本文件-CSDN博客

最近有2点更新,一是有一些pdf文件转换出来的图片是横的,这样也可以识别文字,但是可能会影响效果,另一个是发现有一些文字识别不出来,看了关于提高PaddleOCR识别准确率的一些优化(一)_如何提高paddleocr识别准确率-CSDN博客发现是图片文件的尺寸太大了,为此将其缩小一半再识别。确实提高了识别率。

代码:

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 25 10:42:39 2024@author: YBK
"""import tkinter as tk
import windnd
from tkinter.messagebox import showinfo
import os
from PIL import Image
import fitz
from fitz import Document as openPDF
import time
import re
from paddleocr import PaddleOCR
import subprocessdef dec_to_36(num):base = [str(x) for x in range(10)] + [chr(x) for x in range(ord('A'),ord("A")+26)]# 前者把 0 ~ 9 转换成字符串存进列表 base 里,后者把 A ~ Z 存进列表l = []if num<0:return "-"+dec_to_36(abs(num))while True:num,rem = divmod(num,36) # 求商 和 留余数l.append(base[rem])if num == 0:return "".join(l[::-1])def nowtime_to_str():#将当前时间戳转化为36进制,约6位字符,减少文件名长度unix_timestamp = int(time.time())return(dec_to_36(unix_timestamp))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)"# 打开pdfdoc = openPDF(path)# 图片计数imgcount = 0lenXREF = 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)# 如果不是对象也不是图片,则continueif not isXObject or not isImage:continueimgcount += 1# 根据索引生成图像pix = fitz.Pixmap(doc, i)# 根据pdf的路径生成图片的名称# new_name = path.replace('\\', '_') + "_img{}.png".format(imgcount)# new_name = new_name.replace(':', '')new_name = os.path.basename(path).replace('.pdf', '_') + "img" + str(imgcount).zfill(3) + ".png"# 如果pix.n<5,可以直接存为PNGif pix.n < 5:pix._writeIMG(os.path.join(pic_path, new_name),1,10)# 否则先转换CMYKelse:pix0 = fitz.Pixmap(fitz.csRGB, pix)pix0._writeIMG(os.path.join(pic_path, new_name),1,10)pix0 = None# 释放资源pix = Noneimage = Image.open(os.path.join(pic_path, new_name))#对于尺寸大于2000 * 2000的图像,缩放至(h * 0.5,w * 0.5)识别准确率有所提升if image.width > 2000 or image.height > 2000:new_image = image.resize((int(image.width * 0.5), int(image.height * 0.5)))new_image.save(os.path.join(pic_path, new_name))print("缩小图片尺寸")new_image.close()image = Image.open(os.path.join(pic_path, new_name))#对于图片宽度大于高度,左旋转if image.width > image.height: rotated_img = image.transpose(Image.ROTATE_90)print("左旋转")rotated_img.save(os.path.join(pic_path, new_name))           image.close()t1 = time.perf_counter()print("运行时间:{}s".format(t1 - t0))print("提取了{}张图片".format(imgcount))
def get_file_size(file_path):# 获取文件的大小(单位为字节)file_size = os.stat(file_path).st_sizereturn file_size
def dragged_files(files):fileurl = ''if len(files) > 1:# print("请拖放一个文件!")showinfo("提示","请拖放一个文件!")else:# print(files[0].decode('gbk'))fileurl = files[0].decode('gbk')# print(os.path.splitext(fileurl)[1])if fileurl != '' and os.path.splitext(fileurl)[1] == '.pdf':pdfpath = fileurlfilename0 = os.path.basename(fileurl).replace('.pdf','') + nowtime_to_str()# filename0 用于生成文件夹和文件名,为了不重复,在后面加入编码后的时间戳pic_path = f'e:\\临时文件夹\\{filename0}\\'if not os.path.exists(pic_path):os.mkdir(pic_path)m = pdf2pic(pdfpath, pic_path)pngpath = pic_pathouttxtpath = 'e:\\临时文件夹\\'+filename0+'.txt'ocr = PaddleOCR(use_angle_cls=True, lang="ch") # need to run only once to download and load model into memorylines = []for filename in os.listdir(pngpath):img_path = pngpath+filenameresult = ocr.ocr(img_path, cls=True)print(img_path)# image = Image.open(img_path).convert('RGB')if result[0] is not None:boxes = [detection[0] for line in result for detection in line] # Nested loop addedtxts = [detection[1][0] for line in result for detection in line] # Nested loop addedscores = [detection[1][1] for line in result for detection in line] # Nested loop addedfor box, txt, score in zip(boxes, txts, scores):if score > 0.7:# lines.append(txt.replace('\n',''))lines.append(txt+'\n')# lines.append('\n')with open(outtxtpath, 'w', encoding='utf-8') as f:f.writelines(line for line in lines)subprocess.run(['notepad.exe', outtxtpath], check=True)if __name__ == '__main__':rootWindow = tk.Tk()rootWindow.title("拖放PDF文件识别文字")rootWindow.geometry("300x120")windnd.hook_dropfiles(rootWindow , func=dragged_files)rootWindow.mainloop()

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 树与图的深度优先遍历(dfs的图论中的应用)
  • CleanClip For Mac 強大的剪貼簿助手Paste替代工具 v2.2.1
  • JVM 语言与生态
  • 408算法题leetcode--第10天
  • 基于Python的人工智能应用案例系列(5):手写数字聚类
  • 【matlab安装】最近换磁盘重装电脑安装matlab遇到几个问题
  • 【C++】list容器的基本使用
  • 音视频入门基础:AAC专题(7)——FFmpeg源码中计算AAC裸流每个packet的size值的实现
  • 【Python语言初识(二)】
  • 快速响应:提升前端页面加载速度技巧的必知策略方案
  • 【React】React18.2.0核心源码解读
  • 01-Mac OS系统如何下载安装Python解释器
  • AI大模型之旅--milvus向量库安装
  • 【Mysql-索引总结】
  • Centos 7 搭建Samba
  • 3.7、@ResponseBody 和 @RestController
  • Angular4 模板式表单用法以及验证
  • C++类的相互关联
  • Java IO学习笔记一
  • Javascript Math对象和Date对象常用方法详解
  • Js基础——数据类型之Null和Undefined
  • Linux中的硬链接与软链接
  • MobX
  • Python学习之路13-记分
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 第2章 网络文档
  • 开发了一款写作软件(OSX,Windows),附带Electron开发指南
  • 聊聊flink的TableFactory
  • 深入 Nginx 之配置篇
  • 文本多行溢出显示...之最后一行不到行尾的解决
  • nb
  • Linux权限管理(week1_day5)--技术流ken
  • Salesforce和SAP Netweaver里数据库表的元数据设计
  • ​queue --- 一个同步的队列类​
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • # 数论-逆元
  • ### RabbitMQ五种工作模式:
  • #AngularJS#$sce.trustAsResourceUrl
  • #我与Java虚拟机的故事#连载14:挑战高薪面试必看
  • (2)MFC+openGL单文档框架glFrame
  • (9)目标检测_SSD的原理
  • (a /b)*c的值
  • (bean配置类的注解开发)学习Spring的第十三天
  • (C语言)球球大作战
  • (php伪随机数生成)[GWCTF 2019]枯燥的抽奖
  • (vue)el-tabs选中最后一项后更新数据后无法展开
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (二)springcloud实战之config配置中心
  • (四)鸿鹄云架构一服务注册中心
  • (一)Linux+Windows下安装ffmpeg
  • (一)Neo4j下载安装以及初次使用
  • (原创)Stanford Machine Learning (by Andrew NG) --- (week 9) Anomaly DetectionRecommender Systems...
  • .NET : 在VS2008中计算代码度量值
  • .net core MVC 通过 Filters 过滤器拦截请求及响应内容