一个去掉PDF背景水印的思路

起因

昨天测试 使用“https://github.com/VikParuchuri/marker” 将 pdf 转 Markdown的过程中,发现转换后的文件中会保护一些背景图片,是转换过程中,程序把背景图识别为了内容。于是想着怎么把背景图片去掉。

背景水印图片的特征

我这里拿到的PDF图片都是文字类型的,背景水印应该都是后期加进去的。

总结出两个特征:

  • 每页都存在相同的背景图片
  • 作为背景覆盖了页面大部分区域

基于上面的特征,写代码将背景移除

  • 代码:
import fitz

def scan_background_images(pdf_path):
    doc = fitz.open(pdf_path)
    num_pages = len(doc)
    image_usage = {}  # 字典来记录每个图片的使用情况和页面面积覆盖

    # 遍历文档中的每一页
    for page in doc:
        page_area = abs(page.rect)  # 计算页面面积
        images = page.get_images(full=True)

        for img in images:
            xref = img[0]
            img_rect = page.get_image_rects(xref)
            img_area = sum([abs(rect) for rect in img_rect])  # 计算该图片在当前页面的总覆盖面积

            if img_area / page_area >= 0.5:  # 判断是否覆盖了超过50%的页面面积
                if xref in image_usage:
                    image_usage[xref]['count'] += 1
                    image_usage[xref]['pages'].add(page.number)
                else:
                    image_usage[xref] = {'count': 1, 'pages': set([page.number])}

    # 确定在至少80%的页面上重复出现的图片
    background_images = []
    threshold = 0.8 * num_pages  # 计算至少需要出现在多少页面上

    for xref, data in image_usage.items():
        if data['count'] >= threshold:
            background_images.append(xref)

    # 移除背景图片
    for page in doc:
        for xref in background_images:
            page.clean_contents()  # 清理页面内容,准备删除操作
            page.delete_image(xref)

    # 保存修改后的PDF
    new_pdf_path = pdf_path.replace('.pdf', '_no_bg.pdf')
    doc.save(new_pdf_path)
    doc.close()

    return new_pdf_path

# 使用函数
pdf_path = "example.pdf"
new_pdf_path = scan_background_images(pdf_path)
print("Modified PDF saved to:", new_pdf_path)
  • 依赖
pip install PyMuPDF

效果

  • 去背景前
    在这里插入图片描述
  • 去背景后
    在这里插入图片描述
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您提供一个基于PyPDF4的程序来去除未知PDF水印。以下是代码: ```python import PyPDF4 def remove_watermark(input_file, output_file): # Open the PDF file with open(input_file, 'rb') as file: pdf_reader = PyPDF4.PdfFileReader(file) # Create a new PDF writer object pdf_writer = PyPDF4.PdfFileWriter() # Loop through each page of the PDF for page_num in range(pdf_reader.getNumPages()): # Get the current page page = pdf_reader.getPage(page_num) # Remove the watermark by overwriting it with a blank rectangle if '/Annots' in page: page_dict = page.getObject() annots = page_dict['/Annots'].getObject() for annot in annots: if '/AP' in annot: annot_dict = annot.getObject() if '/N' in annot_dict['/AP']: stream = annot_dict['/AP']['/N'].getObject() if '/BBox' in stream: stream.remove('/BBox') stream.remove('/Matrix') stream.stream = b'q 1 0 0 1 0 0 cm /Im1 Do Q' annot.update() # Add the updated page to the new PDF writer object pdf_writer.addPage(page) # Write the updated PDF to a file with open(output_file, 'wb') as output: pdf_writer.write(output) # Test the program input_file = 'input.pdf' output_file = 'output.pdf' remove_watermark(input_file, output_file) ``` 使用该程序时,将输入文件和输出文件的文件路径传递给`remove_watermark`函数即可。该程序会打开输入文件,遍历每一页,检查是否存在水印,如果存在,则将其用空白矩形覆盖。最后,将更新后的页面添加到新的PDF文件中并保存为输出文件。 请注意,此程序仅适用于某些类型的PDF水印。对于其他类型的水印,可能需要不同的处理方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dapeng-大鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值