日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

PDF转图片实现方式

發(fā)布時(shí)間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PDF转图片实现方式 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 需要python包

PyPDF2

glob

pdf2image

numpy

2.?PyPDF2轉(zhuǎn)圖片步驟

  • 使用PdfFileWriter類(lèi)可以用來(lái)生成一個(gè)新的PDF對(duì)象pdf_writer。
  • PdfFileReader是PyPDF2包的一個(gè)類(lèi),擁有與PDF文檔交互的多種方法,調(diào)用了該類(lèi)創(chuàng)造了一個(gè)可用來(lái)讀取的對(duì)象,這個(gè)對(duì)象的名稱(chēng)為pdf?。
  • 調(diào)用pdf對(duì)象的方法getPage()方法去取得就PDF文件的目標(biāo)頁(yè),下面代碼中我用來(lái)獲取PDF第一頁(yè),也就是此方法的參數(shù)為整數(shù)零,此方法返回一個(gè)頁(yè)面對(duì)象,名稱(chēng)為page。
  • 調(diào)用pdf_writer.addPage(page)將步驟3中page對(duì)象加入pdf_writer列表。
  • 調(diào)用pdf_writer.write(outfile)將pdf_writer列表中指定頁(yè)面保存到臨時(shí)文件中outfile。
  • 調(diào)用pdf2image中convert_from_bytes方法將5中臨時(shí)文件中outfile生成目標(biāo)圖片。
  • 3. 需要按照依賴(lài)poppler

    Poppler是用于呈現(xiàn)可移植文檔格式(PDF)文檔的免費(fèi)軟件實(shí)用程序庫(kù)。不同機(jī)器安裝poppler方式如下:

    3.1 mac機(jī)器

  • Press?Command+Space?and type?Terminal?and press?enter/return?key.
  • Copy and paste the following command in Terminal app:
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    and press?enter/return?key. Wait for the command to finish. If you are prompted to enter a password, please type your Mac user's login password and press ENTER. Mind you, as you type your password, it won't be visible on your Terminal (for security reasons), but rest assured it will work.
  • Now, copy/paste and run this command to make?brew?command available inside the Terminal:?echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
  • Copy and paste the following command:
    brew install poppler
  • Done! You can now use?poppler.

    3.2? window安裝

    Poppler for Windows

    3.3 linux安裝

    apt-get install python-poppler?

    或者

    sudo apt-get install python3-poppler-qt4

    4. Python實(shí)現(xiàn)轉(zhuǎn)圖片代碼

    from PyPDF2 import PdfFileReader, PdfFileWriter import glob import os from pdf2image import convert_from_bytes import shutil import numpy as np from time import timedef pdf2image2(pdfPath, imagePath):images = convert_from_bytes(open(pdfPath, 'rb').read())for image in images:if not os.path.exists(imagePath):os.makedirs(imagePath)pngname=pdfPath[6:-4]image.save(imagePath+'/'+pngname+'.jpg', 'JPEG',quality=30)def process_bar(no, total_length):bar = '\r' + str(no) + '|' + str(total_length)print(bar, end='', flush=True)def split_combine(path, pdf_writer):pdf = PdfFileReader(path, strict=False)# lastest pagepage = pdf.getPage(0)pdf_writer.addPage(page)def run():# get curren dir pdf filesstart_time = time()pdf_list = glob.glob('*.pdf')pdf_writer = PdfFileWriter()imgpath="./img/"tmppath="./tmp/"if not os.path.exists(imgpath):os.makedirs(imgpath)if not os.path.exists(tmppath):os.makedirs(tmppath)for i, pdf_file in enumerate(pdf_list):process_bar(i + 1, len(pdf_list))split_combine(pdf_file, pdf_writer)with open(tmppath+pdf_file, 'wb') as output_pdf:pdf_writer.write(output_pdf)pdf2image2(tmppath+pdf_file, imgpath)shutil.rmtree(tmppath)end_time = time()print(end_time-start_time)if __name__ == '__main__':run()

    總結(jié)

    以上是生活随笔為你收集整理的PDF转图片实现方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。