python 图片添加水印 pdf 添加水印

 1 # /usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # 本示例使用两个第三方库来实现为PDF文件添加文字水印
 4 # 这两个库是pyPdf和reportlab
 5 # 使用的Python版本是Python 2.7
 6 # http://pybrary.net/pyPdf
 7 # http://www.reportlab.com
 8 
 9 import os
10 # 库文件的导入
11 from pyPdf import PdfFileWriter, PdfFileReader
12 from reportlab.pdfgen import canvas
13 
14 from PIL import Image, ImageDraw, ImageFont
15 import os
16 
17 # 指定要使用的字体和大小;/Library/Fonts/是macOS字体目录;Linux的字体目录是/usr/share/fonts/
18 font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf', 45)
19 
20 
21 # image: 图片  text:要添加的文本 font:字体
22 def add_text_to_image(image_name, text, font=font):
23     image = Image.open(image_name)
24     rgba_image = image.convert('RGBA')
25     text_overlay = Image.new('RGBA', rgba_image.size, (255, 255, 255, 0))
26     image_draw = ImageDraw.Draw(text_overlay)
27     text_size_x, text_size_y = image_draw.textsize(text, font=font)
28     # 设置文本文字位置
29     print(rgba_image)
30     # text_xy = (rgba_image.size[0] - text_size_x, rgba_image.size[1] - text_size_y)  #底部
31     # text_xy = ((rgba_image.size[0] - text_size_x) / 2, (rgba_image.size[1] - text_size_y) / 2)  # 中间
32     text_xy = ((rgba_image.size[0] - text_size_x), 0)  # 中间
33     # 设置文本颜色和透明度
34     # image_draw.text(text_xy, text, font=font, fill=(76, 234, 124, 180))
35     image_draw.text(text_xy, text, font=font, fill=(0, 0, 0))
36 
37     image_with_text = Image.alpha_composite(rgba_image, text_overlay)
38 
39     return image_with_text
40 
41 def add_mark(info_name, text):
42     print info_name
43     # 使用reportlab来创建一个PDF文件来作为一个水印文件
44     c = canvas.Canvas("watermark.pdf")
45     c.setFont("Courier", 35)
46 
47     # 设置水印文字的灰度
48     c.setFillGray(0.0, 1)
49 
50     # 设置水印文件,并将文字倾斜45度角
51     c.saveState()
52     c.translate(280, 750)
53     c.rotate(0)
54     c.drawCentredString(0, 0, text)
55     c.restoreState()
56     c.save()
57 
58     output = PdfFileWriter()
59     input1 = PdfFileReader(file(info_name, 'rb'))
60     water = PdfFileReader(file('watermark.pdf', 'rb'))
61 
62     # 获取pdf文件的页数
63     pageNum = input1.getNumPages()
64     print pageNum
65     # 给每一页打水印
66     for i in range(pageNum):
67         page = input1.getPage(i)
68         page.mergePage(water.getPage(0))
69         output.addPage(page)
70     return output
71 
72 def run():
73     os.mkdir("results")
74     file_name = "info"
75     files = os.listdir(file_name)
76     r = 0
77     for t_file in files:
78         file_all_img = file_name + os.sep + t_file
79         if os.path.isfile(file_all_img):
80             continue
81         for file_img in os.listdir(file_all_img):
82             file_img_all_img_all = file_all_img + os.sep + file_img
83             if file_img_all_img_all.endswith('pdf'):
84                 out_put = add_mark(file_img_all_img_all, t_file)
85                 outStream = file('results' + os.sep + '{}.pdf'.format(r), 'wb')
86                 out_put.write(outStream)
87                 outStream.close()
88                 os.remove("watermark.pdf")
89             else:
90                 im_after = add_text_to_image(image_name=file_img_all_img_all, text=t_file)
91                 im_after.save("results/{}.png".format(r))
92             r += 1
93 
94 
95 
96 if __name__ == '__main__':
97     run()
View Code
原文地址:https://www.cnblogs.com/kayb/p/10846341.html