pip install pdf2image
下载依赖文件poppler
上代码
# 导入所需的库
from PIL import Image
import fitz
from pdf2image import convert_from_path
# 定义PDF文件路径、保存图像路径和Poppler路径
pdf_file = r'D:\workspace\python学习笔记.pdf'
save_path = r'D:\workspace\long_image.png'
poppler_path = r"D:\app\poppler_2\bin"
# 将PDF文件转换为图像列表
image_list = convert_from_path(pdf_file, poppler_path=poppler_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(save_path)