pdf转图片(利用pdf2image包)
参考: pdf2image
pip install pdf2image
代码:
from pdf2image import convert_from_path, convert_from_bytes
import os
output_folder = './xx/'
dpi_value= 600
pdf_start_page = 1 # pdf显示的第一页
start_page = 1 # 真实页码
prex = '' # 图像前缀
def to_image():
def page_generator():# 定义图像前缀
while True:
yield prex
images = convert_from_path(r'xx.pdf', dpi=600, output_folder=output_folder,
first_page=pdf_start_page, fmt='jpg', thread_count=6, output_file=page_generator())
def image_rename():
# 文件重命名
global start_page
file_list = os.listdir(output_folder)
for file in file_list:
start_page_str = str(start_page)
if len(start_page_str)==1:
start_page_str = '00'+start_page_str
elif len(start_page_str)==2:
start_page_str = '0'+start_page_str
else:
...
path = os.path.join(output_folder, file)
print(os.path.join(output_folder, start_page_str + '.jpg'))
os.rename(path, os.path.join(output_folder, start_page_str + '.jpg'))
start_page = start_page + 1
to_image()
image_rename() #文件重命名
效果:非常快