当前位置: 首页 > news >正文

多页pdf转长图

单页pdf直接打印-onenote-在该页右键,另存为图片即可。

多页pdf,期望保存为一张图片,直接可用的都需要money。可通过python库来完成:

import os
from pdf2image import convert_from_path
from PIL import Image, ImageDraw


def pdf_to_images(pdf_path, img_path):
    # 转换 PDF 文件为图片
    images = convert_from_path(pdf_path)
    # 遍历图片列表并保存
    paths = []
    for i, image in enumerate(images):
        filePath = os.path.join(img_path, f"page_{i + 1}.jpg")
        if not os.path.exists(img_path):
            os.mkdir(img_path)
        image.save(filePath, "JPEG")
        paths.append(filePath)
    return paths

def stitch_images_vertically(image_paths, output_path, line_color="black", line_height=2):
    """
    将多张图片垂直拼接为一张长图,并在每页末尾添加分割线
    :param image_paths: 图片路径列表
    :param output_path: 输出图片的路径
    :param line_color: 分割线的颜色,默认为黑色
    :param line_height: 分割线的高度,默认为2像素
    """
    # 打开所有图片并获取宽度和高度
    images = [Image.open(img) for img in image_paths]
    widths, heights = zip(*(img.size for img in images))

    # 确定拼接后的图片宽度(取最大宽度)和总高度(包括分割线)
    total_width = max(widths)
    total_height = sum(heights) + line_height * (len(images) - 1)

    # 创建一个新的长图
    new_image = Image.new('RGB', (total_width, total_height), (255, 255, 255))
    draw = ImageDraw.Draw(new_image)

    # 将每张图片粘贴到长图中,并在每页末尾添加分割线
    y_offset = 0
    for img in images[:-1]:  # 不在最后一张图片后添加分割线
        new_image.paste(img, (0, y_offset))
        y_offset += img.height
        # 绘制分割线
        draw.line([(0, y_offset), (total_width, y_offset)], fill=line_color, width=line_height)
        y_offset += line_height

    # 粘贴最后一张图片
    new_image.paste(images[-1], (0, y_offset))

    # 保存长图
    new_image.save(output_path)

# 示例调用
image_paths = pdf_to_images("a.pdf", "./outputs")
output_path = "stitched_image_with_lines.png"  # 替换为输出图片路径
stitch_images_vertically(image_paths, output_path)

若保存的图片过大,可压缩下。比如:

def compress_image_to_target_size(input_image_path, output_image_path, target_size, step=5):
    """
    将图片压缩到指定大小
    :param input_image_path: 输入图片路径
    :param output_image_path: 输出图片路径
    :param target_size: 目标大小(单位:字节)
    :param step: 质量调整步长
    """
    quality = 95  # 初始质量
    while True:
        with Image.open(input_image_path) as img:
            # 保存图片并调整质量
            img.save(output_image_path, quality=quality)

        # 检查文件大小
        file_size = os.path.getsize(output_image_path)
        if file_size <= target_size or quality <= 0:
            break

        # 调整质量
        quality -= step

    print(f"图片已压缩到 {file_size / (1024 * 1024):.2f} MB")

# 示例调用
compress_image_to_target_size("stitched_image_with_lines.png", "output.jpg", 2 * 1024 * 1024)

相关文章:

  • 《重新审视深度部分标签学习中的一致性正则化》2022年ICML论文精读
  • 寄生虫仿生算法:基于寄生虫特征的算法设计
  • c语言整理
  • 重塑音乐未来:张漾斌引领漫寻音乐的颠覆之旅
  • STL标准库
  • 外星人入侵-Python-三
  • 结构型模式之组合模式:让对象构成树形结构
  • AtCoder Beginner Contest 004(A - 流行、B - 回転、C - 入れ替え、D - マーブル)题目翻译
  • Keepalived高可用架构实战:从安装配置到高级应用详解
  • 打包当前Ubuntu镜像 制作Ubuntu togo系统
  • Oracle SQL*Loader 与 SQLULDR2 学习指南
  • 服务器部署RocketMQ----Docker方式
  • LLM后训练:解锁大型语言模型推理能力的关键路径
  • 软件测试之单元测试unittest库使用、参数化、unittestteport
  • Java中的volatile关键字
  • AI自动文献综述——python先把知网的文献转excel
  • 免费高质量贴图(Textures) 网站推荐
  • RabbitMq C++客户端的使用
  • DeepSeek大模型在政务服务领域的应用
  • 四种 No-SQL
  • CBA官方对孙铭徽罚款3万、广厦投资人楼明停赛2场罚款5万
  • 浙江演艺集团7部作品组团来沪,今夏开启首届上海演出季
  • 前四个月社会融资规模增量累计为16.34万亿元,比上年同期多3.61万亿元
  • 国台办:实现祖国完全统一是大势所趋、大义所在、民心所向
  • 中巴续签双边本币互换协议,进一步深化金融战略合作
  • 三亚通报救护车省外拉警报器开道旅游:违规违法,责令公司停业整顿