安装pdf2image
mac系统,安装pdf2image依赖
brew install poppler
安装库
pip3 install pdf2image
pip3 install Pillow
运行脚本
from PIL import Image
from pdf2image import convert_from_path
pdf_path = ""
output_image_path = ""
# 将PDF文件转换为图像列表
image_list = convert_from_path(pdf_path)
# 获取第一个图像的尺寸
page_width, page_height = image_list[0].size
# 创建一个与所有图像大小相同的画布
merged_image = Image.new("RGB", (page_width, page_height * len(image_list)), (255, 255, 255))
# 将所有图像粘贴到画布上
for i, image in enumerate(image_list):
merged_image.paste(image, (0, i * page_height))
# 将画布保存为一张长图
merged_image.save(output_image_path)