【Python】论文长截图、页面分割、水印去除、整合PDF

有的学校的论文只能在线预览,且存在水印。为保存到本地方便查阅,可以使用以下工作流进行处理:

  1. 用浏览器打开在线论文预览界面;
  2. 使用fastone capture软件截长图;
  3. 将论文按页数进行分割;
  4. 按照阈值消除浅色的背景水印;
  5. 整合为A4尺寸的PDF文件;
  6. 使用WPS将PDF转为OCR版本(可选)。

以下代码为上述流程的第三、四、五步,注释都在代码中,随取随用。

python 复制代码
import os
from PIL import Image
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
import numpy as np

def process_image(input_image_path, output_dir="split_images", total_pages=138, watermark_threshold=230):
    """
    处理图片并生成PDF
    
    Args:
        input_image_path: 输入图片路径
        output_dir: 输出目录路径
        total_pages: 总页数
        watermark_threshold: 水印识别阈值
    """
    # 创建输出文件夹
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # 打开原始图片
    img = Image.open(input_image_path)
    width, height = img.size

    # 计算新的高度(total_pages的整数倍)
    new_height = ((height + total_pages - 1) // total_pages) * total_pages
    img = img.resize((width, new_height), Image.Resampling.LANCZOS)
    height = new_height

    # 计算每份高度
    slice_height = height // total_pages
    remaining_height = height % total_pages  # 此时应该为0

    # 分割并保存图片
    image_files = []
    for i in range(total_pages):
        # 计算当前切片的位置
        top = i * slice_height
        bottom = top + slice_height
        if i == total_pages - 1:  # 最后一片加上余数
            bottom += remaining_height
        
        # 裁剪图片
        slice_img = img.crop((0, top, width, bottom))
        
        # 去除水印处理
        img_array = np.array(slice_img)
        
        # 设置阈值来识别水印
        mask = np.all(img_array > watermark_threshold, axis=2)
        
        # 将水印区域替换为背景色
        img_array[mask] = [255, 255, 255]  # 替换为白色
        
        # 转回PIL图片
        processed_img = Image.fromarray(img_array)
        
        # 保存处理后的切片
        output_file = os.path.join(output_dir, f"slice_{i+1:03d}.png")
        processed_img.save(output_file)
        image_files.append(output_file)

    return image_files

def create_pdf(image_files, pdf_file="combined_output.pdf"):
    """
    将图片合并为PDF
    
    Args:
        image_files: 图片文件路径列表
        pdf_file: 输出PDF文件路径
    """
    c = canvas.Canvas(pdf_file, pagesize=A4)
    a4_width, a4_height = A4

    for img_file in image_files:
        img = Image.open(img_file)
        # 计算缩放比例以适应A4纸张
        aspect = img.width / img.height
        if aspect > A4[0] / A4[1]:  # 如果图片太宽
            new_width = a4_width
            new_height = new_width / aspect
        else:  # 如果图片太高
            new_height = a4_height
            new_width = new_height * aspect
        
        # 居中放置图片
        x = (a4_width - new_width) / 2
        y = (a4_height - new_height) / 2
        
        # 添加图片到PDF
        c.drawImage(img_file, x, y, width=new_width, height=new_height)
        c.showPage()

    c.save()

def main(input_image_path, output_dir="split_images", pdf_file="combined_output.pdf", 
            total_pages=138, watermark_threshold=230):
    """
    主函数
    
    Args:
        input_image_path: 输入图片路径
        output_dir: 输出目录路径
        pdf_file: 输出PDF文件路径
        total_pages: 总页数
        watermark_threshold: 水印识别阈值
    """
    image_files = process_image(input_image_path, output_dir, total_pages, watermark_threshold)
    create_pdf(image_files, pdf_file)
    
    print("处理完成!")
    print(f"切片图片保存在: {output_dir}")
    print(f"PDF文件保存为: {pdf_file}")

if __name__ == "__main__":
    # 示例使用
    input_path = r"C:\Users\Administrator\Desktop\test\2025-01-06_102531.png"
    main(input_path)

最后得到的PDF是图片格式的,可以使用WPS转为OCR版本,可以直接划取文字。

相关推荐
Rust研习社4 分钟前
Rust 智能指针 Cell 与 RefCell 的内部可变性
开发语言·后端·rust
2301_8135995532 分钟前
HTML图片怎么用UnoCSS对齐_UnoCSS原子化CSS图片对齐实战
jvm·数据库·python
m0_3776182335 分钟前
c++怎么在不加载整个大文件的情况下获取其SHA256校验值【进阶】
jvm·数据库·python
LN花开富贵37 分钟前
【ROS】鱼香ROS2学习笔记二
linux·笔记·python·学习·嵌入式
qq_1898070341 分钟前
CSS如何实现纯CSS树状目录结构_利用-checked与递归思维构建交互节点
jvm·数据库·python
Micr06742 分钟前
利用Werkzeug-Debug实现本地权限提升
python·web安全·网络安全
leaves falling44 分钟前
C++模板进阶
开发语言·c++
yanghuashuiyue1 小时前
langchain AI应用框架研究【开发部署-篇四】
python·langchain
无敌昊哥战神1 小时前
【保姆级题解】力扣17. 电话号码的字母组合 (回溯算法经典入门) | Python/C/C++多语言详解
c语言·c++·python·算法·leetcode
2301_777599371 小时前
Go语言如何做HTTP连接池_Go语言HTTP连接池教程【最新】
jvm·数据库·python