当前位置:网站首页>PDF转图片实现方式
PDF转图片实现方式
2022-06-22 06:57:00 【&永恒的星河&】
1. 需要python包
PyPDF2
glob
pdf2image
numpy
2. PyPDF2转图片步骤
- 使用PdfFileWriter类可以用来生成一个新的PDF对象pdf_writer。
- PdfFileReader是PyPDF2包的一个类,拥有与PDF文档交互的多种方法,调用了该类创造了一个可用来读取的对象,这个对象的名称为pdf 。
- 调用pdf对象的方法getPage()方法去取得就PDF文件的目标页,下面代码中我用来获取PDF第一页,也就是此方法的参数为整数零,此方法返回一个页面对象,名称为page。
- 调用pdf_writer.addPage(page)将步骤3中page对象加入pdf_writer列表。
- 调用pdf_writer.write(outfile)将pdf_writer列表中指定页面保存到临时文件中outfile。
- 调用pdf2image中convert_from_bytes方法将5中临时文件中outfile生成目标图片。
3. 需要按照依赖poppler
Poppler是用于呈现可移植文档格式(PDF)文档的免费软件实用程序库。不同机器安装poppler方式如下:
3.1 mac机器
- Press
Command+Spaceand 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 popplerDone! You can now use
poppler.
3.2 window安装
3.3 linux安装
apt-get install python-poppler
或者
sudo apt-get install python3-poppler-qt4
4. Python实现转图片代码
from PyPDF2 import PdfFileReader, PdfFileWriter
import glob
import os
from pdf2image import convert_from_bytes
import shutil
import numpy as np
from time import time
def 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 page
page = pdf.getPage(0)
pdf_writer.addPage(page)
def run():
# get curren dir pdf files
start_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()
边栏推荐
- 仙人掌之歌——进军To C直播(1)
- Vector of relevant knowledge of STL Standard Template Library
- SQL injection vulnerability (x) secondary injection
- Dijin introduces digi connectcore voice control software for connectcore system module
- [out of distribution detection] deep analog detection with outlier exposure ICLR '19
- 首次用DBeaver连接mysql报错
- How to learn 32-bit MCU
- 《数据安全实践指南》- 数据采集安全管理
- OpenGL - Textures
- cookie的介绍和使用
猜你喜欢

Theory and application of naturallanguageprocessing
![[distributed external detection] Odin ICLR '18](/img/8b/f1984f814d69391d80d251cfed0b09.jpg)
[distributed external detection] Odin ICLR '18

Self supervised learning for general out of distribution detection AAAI '20
![[Gan] Introduction to Gan basics and dcgan](/img/93/f0287f93283707b7082630cb89daf1.jpg)
[Gan] Introduction to Gan basics and dcgan
![[out of distribution detection] learning confidence for out of distribution detection in neural networks arXiv '18](/img/07/d5479dde181c355d95c73e2f58c0da.jpg)
[out of distribution detection] learning confidence for out of distribution detection in neural networks arXiv '18
![[outside distribution detection] your classifier is secret an energy based model and you head treat it like one ICLR '20](/img/ec/605f5fcaaebd4829ab555626a9a3e9.jpg)
[outside distribution detection] your classifier is secret an energy based model and you head treat it like one ICLR '20

Detailed tutorial on connecting MySQL with tableau
![[fundamentals of machine learning 02] decision tree and random forest](/img/24/28964279ea479476e1a232aec9c599.jpg)
[fundamentals of machine learning 02] decision tree and random forest

【GAN】W-GAN ICLR‘17, ICML‘17
![[meta learning] classic work MAML and reply (Demo understands meta learning mechanism)](/img/e5/ea68e197834ddcfe10a14e631c68d6.jpg)
[meta learning] classic work MAML and reply (Demo understands meta learning mechanism)
随机推荐
2022-06-21:golang选择题,以下golang代码输出什么?A:3;B:4;C:100;D:编译失败。 package main import ( “fmt“ ) func
如何才能有效缓解焦虑?看看猿辅导怎么说
Leetcode: interview question 08.12 Eight queens [DFS + backtrack]
Languo technology helps the ecological prosperity of openharmony
[tp6] using the workman websocket
golang调用sdl2,播放pcm音频,报错signal arrived during external code execution。
golang調用sdl2,播放pcm音頻,報錯signal arrived during external code execution。
[php] composer installation
Training penetration range 02 | 3-star VH LLL target | vulnhub range node1
Tableau 连接mysql详细教程
Tp6 links to sqlserver, PHP links to sqlserver, and Linux offline installation and deployment
QT connect to Alibaba cloud using mqtt protocol
深度解析Optimism被盗2000万个OP事件(含代码)
[openairinterface5g] RRC NR resolution (I)
Self supervised learning for general out of distribution detection AAAI '20
Laravel excel 3.1 column width setting does not work
Cactus Song - March to C live broadcast (3)
OpenGL - Textures
猿辅导最强暑假计划分享:暑假计划这样做,学习玩耍两不误
仙人掌之歌——上线运营(4)