출처] https://herohjk.com/41
import os
from PIL import Image
from reportlab.pdfgen.canvas import Canvas
from PyPDF2 import PdfReader, PdfWriter
class Watermark:
def __init__(self, output_path, image_path, src_path, working_dir):
self.output_path = output_path
self.image_path = image_path
self.src_path = src_path
self.working_dir = working_dir
if not os.path.exists(self.working_dir):
os.makedirs(self.working_dir)
@staticmethod
def clear_white_background(image):
"""
:param image:
:return:
"""
image = image.convert('RGBA')
image_data = image.getdata()
new_image_data = []
for pixel in image_data:
if pixel[0] > 240 and pixel[1] > 240 and pixel[2] > 240:
new_image_data.append((0, 0, 0, 0))
else:
new_image_data.append(pixel)
image.putdata(new_image_data)
return image
@staticmethod
def image_to_pdf(image_path, pdf_path):
"""
:param image_path:
:param pdf_path:
:return:
"""
size = Image.open(image_path, 'r').size
new_canvas = Canvas(pdf_path, pagesize=Image.open(image_path, 'r').size)
new_canvas.drawImage(image=image_path, x=0, y=0, mask='auto')
new_canvas.save()
@staticmethod
def pdf_merge(save_path, pdf_path, watermark_pdf_path):
"""
:param save_path:
:param pdf_path:
:param watermark_pdf_path:
:return:
"""
pdf_file = open(pdf_path, 'rb')
pdf_reader = PdfReader(pdf_file, strict=False)
watermark_pdf_file = open(watermark_pdf_path, 'rb')
watermark_pdf = PdfReader(watermark_pdf_file, strict=False).pages[0]
pdf_writer = PdfWriter()
for pageNum in range(len(pdf_reader.pages)):
page_obj = pdf_reader.pages[pageNum]
x = (page_obj.mediabox[2] - watermark_pdf.mediabox[2]) / 2
y = (page_obj.mediabox[3] - watermark_pdf.mediabox[3]) / 2
watermark_pdf.add_transformation(Transformation().translate(tx=x, ty=y))
page_obj.merge_page(page2=watermark_pdf, expand=False)
pdf_writer.add_page(page_obj)
result_file = open(save_path, 'wb')
pdf_writer.write(result_file)
def convert(self):
"""
:return:
"""
image = Image.open(self.image_path, 'r')
clear_image = self.clear_white_background(image)
clear_image_path = os.path.join(self.working_dir, 'clear_image.png')
clear_image.save(clear_image_path)
watermark_pdf_path = os.path.join(self.working_dir, 'watermark_pdf.pdf')
self.image_to_pdf(clear_image_path, watermark_pdf_path)
self.pdf_merge(self.output_path, self.src_path, watermark_pdf_path)