from pdf2image import convert_from_pathfrom PIL import Imageimport ospdf_path = r"G:\text.pdf" # PDF 文件路径output_path = "longpicture.png" # 输出的长图文件名poppler_path = r"G:\poppler\Library\bin" # Poppler 路径(根据你实际解压位置修改)# 1. 将 PDF 每一页转为图片列表print("正在转换 PDF,请稍候...")images = convert_from_path(pdf_path, dpi=300, poppler_path=poppler_path)if not images: print("未找到 PDF 页面,请检查文件路径") exit()print(f"共 {len(images)} 页,开始拼接长图...")# 2. 获取单页尺寸(假设所有页面尺寸相同)page_width, page_height = images[0].size# 3. 创建一张足够长的空白画布total_height = page_height * len(images)long_image = Image.new("RGB", (page_width, total_height), color=(255, 255, 255))# 4. 把每一页拼接到画布上for i, img in enumerate(images): y_offset = i * page_height long_image.paste(img, (0, y_offset)) print(f"已拼接第 {i+1}/{len(images)} 页")# 5. 保存长图long_image.save(output_path)print(f"✅ 成功生成:{output_path}")