Python 多进程实现jpg转pdf

这篇博客介绍了如何使用Python的Pillow库将图片批量转换为PDF,并利用CnOcr进行OCR识别。通过多线程和tqdm库实现进程跟踪,提高了效率。此外,还展示了如何在多线程中应用进度条展示转换进度。
摘要由CSDN通过智能技术生成
  • 安装pillow库(PIL)
    • pip install pillow
  •  实现jpg 转 pdf
    • from PIL import Image
      
      
      def jpg_pdf(img_path, pdf_name):
          """
          jpg图片 convert PDF
          :param img: 图片路径
          :return: PDF名字 eg:a.pdf
          """
          image = Image.open(img_path)
          img = image.convert('RGB')
          img.save(pdf_name)
  •  将单个函数添加到多线程中(图片转PDF感觉是io操作多一些,这里使用多线程)
    • from concurrent.futures import ThreadPoolExecutor
      import os
      
      
      def main(img_path):
          with ThreadPoolExecutor(max_workers=17) as pool:
              pool.map(jpg_pdf, os.listdir(img_path))
    • 这里的pool.map() 后面传的是函数和列表这种格式

    •  多线程加进度条显示可以直观的看出进行了多少

      • from tqdm import tqdm
        
        
        with futures.ThreadPoolExecutor(max_workers=17) as pool:
            list(tqdm(pool.map(jpg_pdf, os.listdir(img_path)), total=len(os.listdir(img_path))))
  •  ocr识别jpg图片
    • from cnocr import CnOcr
      import numpy as np
      
      
      
      def ocr(img):
          """
            img:PIL.Image.open()
          """
          image = np.array(img.convert("L"))
          res = CnOcr().ocr_for_single_line(image)
          return "".join(res)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值