python PDF转图片
准备工作
下载模块pdf2image
pip install pdf2image
下载依赖(不下载会报错)
-
Mac
brew install poppler
代码实现
from pdf2image import convert_from_path, convert_from_bytes
import tempfile
file_path = '/Users/zonghan/Downloads/upload.pdf'
# 方法一
pages = convert_from_path(file_path, dpi=500) # dpi是输出的质量
for page in pages:
print(page)
print(pages.index(page)) # 页面索引
page.save('/Users/zonghan/Desktop/out.jpg', 'JPEG')
# 方法二
images = convert_from_bytes(open(file_path, 'rb').read())
for page in pages:
print(page)
print(pages.index(page)) # 页面索引
page.save('/Users/zonghan/Desktop/out.jpg', 'JPEG')
# 方法三(推荐)
with tempfile.TemporaryDirectory() as path:
pages = convert_from_path(file_path, output_folder=path, dpi=500)
for page in pages:
print(page)
print(pages.index(page)) # 页面索引
page.save('/Users/zonghan/Desktop/out.jpg', 'PNG')
这样就会在指定的路径保存文件
标签:convert,python,page,file,path,print,PDF,pages,图片 From: https://www.cnblogs.com/zonghan/p/16869396.html