当前位置:   article > 正文

python导出PDF文件中的图片_lenxrf

lenxrf

直接上代码,不过要先安装pymupdf,使用pymupdf中的fitz导出PDF中的图片

pip install pymupdf

代码:

  1. import fitz
  2. import re
  3. import os
  4. import sys
  5. import _thread
  6. import threading
  7. import time
  8. # PDF转图像函数
  9. def pdf2image(path, pic_path):
  10. checkIM = r"/Subtype(?= */Image)"
  11. pdf = fitz.open(path)
  12. lenXREF = pdf.xref_length()
  13. count = 1
  14. imgThreadList = []
  15. # 创建目录
  16. if not os.path.exists(pic_path):
  17. os.mkdir(pic_path)
  18. # 遍历所有对象
  19. for i in range(1, lenXREF):
  20. text = pdf.xref_object(i)
  21. isImage = re.search(checkIM, text)
  22. if not isImage:
  23. continue
  24. print('开始转换 ' + str(i) + '/' + str(lenXREF))
  25. pix = fitz.Pixmap(pdf, i)
  26. # 判断图像大小
  27. if pix.w < 100 or pix.h < 100:
  28. print('图像过小(' + str(pix.w()) + ',' + str(pix.h()) + ')')
  29. continue
  30. # 图像文件路径名称
  31. new_name = os.path.join(pic_path, f"img_{count}.png")
  32. clock1 = time.monotonic()
  33. # 直接保存
  34. #pix.writePNG(new_name) #废弃
  35. pix.save(new_name)
  36. clock2 = time.monotonic()
  37. print('转换完成(time:' + str(clock2-clock1) + ') ' + str(i) + '/' + str(lenXREF) + ' 图片 -> ' + str(count) + ': ' + new_name)
  38. count += 1
  39. pix = None
  40. #####################################################################################################
  41. if __name__=='__main__':
  42. file_path = r'5.pdf' # PDF 文件路径
  43. dir_path = r'pics' # 存放图片的文件夹
  44. # 文件当前路径
  45. print(os.path.split(sys.argv[0])[0])
  46. print(os.path.split(os.path.realpath(__file__))[0])
  47. curPath = os.path.split(os.path.realpath(__file__))[0]
  48. # 开始转换指定文件
  49. pdf2image(os.path.join(curPath, file_path), os.path.join(curPath, dir_path))

多线程版,实际尝试了一下似乎没什么效果,暂时只保留代码

  1. import fitz
  2. import re
  3. import os
  4. import sys
  5. import _thread
  6. import threading
  7. import time
  8. # 自定义保存图片线程类
  9. class myThread (threading.Thread):
  10. def __init__(self, pdfImg, imgFileName):
  11. threading.Thread.__init__(self)
  12. self.pdfImg = pdfImg
  13. self.imgFileName = imgFileName
  14. def run(self):
  15. print ("开始线程:" + self.imgFileName)
  16. time.sleep(1)
  17. self.pdfImg.save(self.imgFileName)
  18. print ("退出线程:" + self.imgFileName)
  19. # 保存图像函数
  20. def mysaveImg(pdfImg, imgFileName):
  21. time.sleep(1)
  22. print ("开始保存图片:" + imgFileName)
  23. pdfImg.save(imgFileName)
  24. print ("结束保存图片:" + imgFileName)
  25. # PDF转图像函数
  26. def pdf2image(path, pic_path):
  27. checkIM = r"/Subtype(?= */Image)"
  28. pdf = fitz.open(path)
  29. lenXREF = pdf.xref_length()
  30. count = 1
  31. imgThreadList = []
  32. # 创建目录
  33. if not os.path.exists(pic_path):
  34. os.mkdir(pic_path)
  35. # 遍历所有对象
  36. for i in range(1, lenXREF):
  37. text = pdf.xref_object(i)
  38. isImage = re.search(checkIM, text)
  39. if not isImage:
  40. continue
  41. print('开始转换 ' + str(i) + '/' + str(lenXREF))
  42. pix = fitz.Pixmap(pdf, i)
  43. # 判断图像大小
  44. if pix.w < 100 or pix.h < 100:
  45. print('图像过小(' + str(pix.w()) + ',' + str(pix.h()) + ')')
  46. continue
  47. # 图像文件路径名称
  48. new_name = os.path.join(pic_path, f"img_{count}.png")
  49. clock1 = time.monotonic()
  50. # 直接保存
  51. #pix.writePNG(new_name) #废弃
  52. #pix.save(new_name)
  53. # 虽然使用了多线程,但是保存还是一张一张的保存,原因未知
  54. #启动一个线程处理
  55. #thread1 = None
  56. #thread1 = myThread(pix, new_name)
  57. #thread1.start()
  58. #thread1 = None
  59. # 线程放入一个列表,等会一起启动
  60. #imgThreadList.append(myThread(pix, new_name))
  61. # 使用线程函数处理
  62. _thread.start_new_thread( mysaveImg, (pix, new_name, ) )
  63. clock2 = time.monotonic()
  64. print('转换完成(time:' + str(clock2-clock1) + ') ' + str(i) + '/' + str(lenXREF) + ' 图片 -> ' + str(count) + ': ' + new_name)
  65. count += 1
  66. pix = None
  67. # 启动所有线程
  68. #for imgThread in imgThreadList:
  69. # imgThread.start()
  70. #####################################################################################################
  71. if __name__=='__main__':
  72. file_path = r'5.pdf' # PDF 文件路径
  73. dir_path = r'5556' # 存放图片的文件夹
  74. # 文件当前路径
  75. print(os.path.split(sys.argv[0])[0])
  76. print(os.path.split(os.path.realpath(__file__))[0])
  77. curPath = os.path.split(os.path.realpath(__file__))[0]
  78. # 开始转换指定文件
  79. #pdf2image(os.path.join(curPath, file_path), os.path.join(curPath, dir_path))
  80. # 转换多个文件
  81. for index in range(1, 5+1, 1):
  82. print('文件:' + os.path.join(curPath, str(index)) + '.pdf')
  83. pdf2image(os.path.join(curPath, str(index)) + '.pdf', os.path.join(curPath, str(index)))

参考:

https://blog.csdn.net/weixin_46737755/article/details/113085763

https://blog.csdn.net/qq_15969343/article/details/81673302

https://blog.csdn.net/weixin_46737755/article/details/113085763

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/51656
推荐阅读
相关标签
  

闽ICP备14008679号