|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | + |
| 4 | +from PIL import Image |
| 5 | +from itertools import product |
| 6 | +import fitz |
| 7 | +import os |
| 8 | + |
| 9 | + |
| 10 | +def remove_img(): |
| 11 | + image_file = input("请输入图片地址:") |
| 12 | + img = Image.open(image_file) |
| 13 | + |
| 14 | + width, height = img.size |
| 15 | + |
| 16 | + for pos in product(range(width), range(height)): |
| 17 | + rgb = img.getpixel(pos)[:3] |
| 18 | + if(sum(rgb) >= 630): |
| 19 | + img.putpixel(pos, (255, 255, 255)) |
| 20 | + |
| 21 | + img.save('d:/qsy.png') |
| 22 | + |
| 23 | + |
| 24 | +def remove_pdf(): |
| 25 | + page_num = 0 |
| 26 | + pdf_file = input("请输入 pdf 地址:") |
| 27 | + pdf = fitz.open(pdf_file); |
| 28 | + for page in pdf: |
| 29 | + pixmap = page.get_pixmap() |
| 30 | + for pos in product(range(pixmap.width), range(pixmap.height)): |
| 31 | + rgb = pixmap.pixel(pos[0], pos[1]) |
| 32 | + if(sum(rgb) >= 630): |
| 33 | + pixmap.set_pixel(pos[0], pos[1], (255, 255, 255)) |
| 34 | + pixmap.pil_save(f"d:/pdf_images/{page_num}.png") |
| 35 | + print(f"第{page_num}水印去除完成") |
| 36 | + page_num = page_num + 1 |
| 37 | + |
| 38 | +def pic2pdf(): |
| 39 | + pic_dir = input("请输入图片文件夹路径:") |
| 40 | + |
| 41 | + pdf = fitz.open() |
| 42 | + img_files = sorted(os.listdir(pic_dir),key=lambda x:int(str(x).split('.')[0])) |
| 43 | + for img in img_files: |
| 44 | + print(img) |
| 45 | + imgdoc = fitz.open(pic_dir + '/' + img) |
| 46 | + pdfbytes = imgdoc.convertToPDF() |
| 47 | + imgpdf = fitz.open("pdf", pdfbytes) |
| 48 | + pdf.insertPDF(imgpdf) |
| 49 | + pdf.save("images.pdf") |
| 50 | + pdf.close() |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + pic2pdf() |
0 commit comments