Skip to main content

A package to remove watermarks from PDF files

Project description

PDF Watermark Remover

A Python package to remove watermarks from PDF files.

Installation

pip install pdf_watermark_remover

Usage

from pdf_watermark_remover import process_pdf

process_pdf('input.pdf', 'output.pdf')

Update PyPI Version

1.更新 setup.py 中的版本号
setup(
    name='your-package-name',
    version='0.7.0',  # 更新版本号
    ...
)
2.重新生成分发文件
rm -rf dist
python setup.py sdist bdist_wheel
3.上传新版本
twine upload dist/*

.pypirc 配置

[distutils]
index-servers =
    pypi

[pypi]
  username = __token__
  password = <your-api-token>

setup.py

from setuptools import setup, find_packages

setup(
    name='pdf_watermark_remover',
    version='0.7.0',
    packages=find_packages(),
    install_requires=[
        'numpy',
        'Pillow',
        'PyMuPDF',
        'reportlab',
    ],
    entry_points={
        'console_scripts': [
            'pdf_watermark_remover=pdf_watermark_remover.remover:process_pdf',
        ],
    },
    author='huapohen',
    author_email='694450321@qq.com',
    description='A package to remove watermarks from PDF files',
    long_description=open('README.md').read(),
    long_description_content_type='text/markdown',
    url='https://github.com/huapohen/pdf_watermark_remover',
    classifiers=[
        'Programming Language :: Python :: 3',
        'License :: OSI Approved :: Apache Software License',
        'Operating System :: OS Independent',
    ],
    python_requires='>=3.6',
)

requirements.txt

# opencv-python-headless
opencv-python
numpy
Pillow
PyMuPDF
reportlabW

准备项目结构

pdf_watermark_remover/
│
├── pdf_watermark_remover/
│   ├── __init__.py
│   ├── remover.py
│
├── setup.py
├── README.md
├── LICENSE
└── requirements.txt

创建项目文件

pdf_watermark_remover/remover.py pdf_watermark_remover/init.py:

from .remover import process_pdf, remove_watermark, pdf_to_images, images_to_pdf

构建和发布包

1.安装必要的工具
pip install setuptools wheel twine
2.构建包
rm -rf dist
python setup.py sdist bdist_wheel
3.发布到PyPI
这里需要配置好.pypirc,填写pypi api token
twine upload dist/*
4.安装
pip install pdf_watermark_remover
5.使用
from pdf_watermark_remover import process_pdf
process_pdf('input.pdf', 'output.pdf')

remover.py

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# pip install pymupdf pillow opencv-python reportlab
import io, os, cv2, fitz, tempfile
import numpy as np
from PIL import Image
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter


def remove_watermark(img, lower_hsv=160, upper_hsv=255):
    # img = cv2.imread("words_watermark.jpg")
    # 从一张图像中去除所有白色内容(或在HSV色彩空间中接近白色的内容)
    # 用的是HSV色彩空间,数组的三个值分别对应Hue(色调),Saturation(饱和度)和Value(亮度)
    lower_hsv = np.array([lower_hsv] * 3)  # 大致对应于白色,偏白
    upper_hsv = np.array([upper_hsv] * 3)  # 最大值,纯白。范围 0~255
    # 根据设定的阈值范围,生成一个二值掩码(mask),其中白色部分表示水印,黑色部分表示非水印区域
    mask = cv2.inRange(img, lower_hsv, upper_hsv)  # 从较亮的灰色(160)到纯白色(255)
    mask = cv2.GaussianBlur(
        mask, (1, 1), 0
    )  # 对生成的掩码进行高斯模糊处理,细节处减少噪点
    mask_indices = mask == 255  # 获取掩码中所有白色像素的索引,160~255以全部重置为255
    img[mask_indices] = [255, 255, 255]  # 使用掩码将原始图片中的水印区域设置为纯白色
    # quality = [int(cv2.IMWRITE_JPEG_QUALITY), 100]  # 保存为最高质量(100)
    # cv2.imwrite('clean.jpg', img, quality)
    return img


def pdf_to_images(pdf_path, dpi=300):
    pdf_document = fitz.open(pdf_path)
    images = []

    for page_num in range(len(pdf_document)):
        page = pdf_document.load_page(page_num)
        zoom = dpi / 72
        mat = fitz.Matrix(zoom, zoom)
        pix = page.get_pixmap(matrix=mat, alpha=False)
        img_bytes = pix.tobytes('png')
        img = Image.open(io.BytesIO(img_bytes))
        images.append(img)

    return images


def images_to_pdf(images, output_pdf_path):
    c = canvas.Canvas(output_pdf_path, pagesize=letter)
    for img in images:
        with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp_img_file:
            img.save(temp_img_file, format='PNG')
            temp_img_file_path = temp_img_file.name
        c.drawImage(temp_img_file_path, 0, 0, width=letter[0], height=letter[1])
        c.showPage()
        os.remove(temp_img_file_path)
    c.save()


def process_pdf(pdf_path, output_pdf_path, lower_hsv=160, upper_hsv=255):
    images = pdf_to_images(pdf_path)
    cleaned_images = []

    for img in images:
        img_np = np.array(img)
        cleaned_img_np = remove_watermark(img_np, lower_hsv, upper_hsv)
        cleaned_img = Image.fromarray(cleaned_img_np)
        cleaned_images.append(cleaned_img)

    images_to_pdf(cleaned_images, output_pdf_path)
    # print(f"处理完成,新的PDF已保存到{output_pdf_path}")


'''
# 示例使用
pdf_path = "example.pdf"  # 你的PDF文件路径
output_pdf_path = "cleaned_example.pdf"  # 保存处理后PDF文件的路径
process_pdf(pdf_path, output_pdf_path)
'''

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pdf_watermark_remover-0.7.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

pdf_watermark_remover-0.7.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file pdf_watermark_remover-0.7.0.tar.gz.

File metadata

  • Download URL: pdf_watermark_remover-0.7.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for pdf_watermark_remover-0.7.0.tar.gz
Algorithm Hash digest
SHA256 1c60f23fce1825fe266d1b8d6571afeca5708ac92792e452b1d99d311cc97930
MD5 b54bd10b37b6cefda9668ec03da8cda2
BLAKE2b-256 7198e9994d8a4304d694faaed83f25ba29b920b3232f9ee5078ef13b59553dab

See more details on using hashes here.

File details

Details for the file pdf_watermark_remover-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pdf_watermark_remover-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 abe85d4c8513701875edf590031c78e2422fb3a816152f05b5aed44ad45fc68e
MD5 063c335ca9f716d4420b37bb79ab9d20
BLAKE2b-256 d62b25fef266b6a5b3867a5056b61eb4f61f6e202f337a33749586a95d9cafa0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page