利用Python实现全局水印

此脚本支持获取当前用户名,在 AD 域里面使用很方便。自备字体文件。

import tkinter as tk
import win32api
import win32con
import pywintypes
import os
from screeninfo import get_monitors
from PIL import Image, ImageDraw, ImageFont,ImageTk

font_family = "msyh.ttf"  # Replace with your actual font filename
font_size = 36
def generate_watermark(text, width, height, color='#d5d5d5', opacity=0, lines=10, angle=0):

    spacing = 100
    # 创建一个 Image 对象,用于绘制水印
    img = Image.new('RGBA', (width, height), (255, 255, 255, 0))
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype(font_family, font_size)

    # 将文本拆分成多行
    text_lines = text.split('\n') if text else lines

    # 计算每个水印的宽度和高度
    bbox = draw.textbbox((0, 0), text_lines[0], font=font)
    text_width, text_height = bbox[2] - bbox[0] + spacing, bbox[3] - bbox[1] + spacing

    # 计算每行可以容纳的水印数量
    watermarks_per_row = width // (text_width + spacing)
    total_rows = (height - 50) // (text_height + spacing)
    # 设置初始位置
    x = 50
    y = 30

    for row in range(0,total_rows):
        x = 50 # 每行重新从左侧开始
        y = 50 + row * (text_height + spacing)

        for col in range(0,watermarks_per_row):

            rotated_text = Image.new('RGBA', (text_width, text_height), (255, 255, 255, 0))
            rotated_draw = ImageDraw.Draw(rotated_text)
            rotated_draw.text((0, 0), text_lines[row % len(text_lines)], font=font, fill=color)
            rotated_text = rotated_text.rotate(angle, expand=True)
            # 将旋转后的文本粘贴到主图像上
            img.paste(rotated_text, (x, y), rotated_text)
            x += text_width + spacing

    tk_img = ImageTk.PhotoImage(img)

    # 创建一个 Label 并显示图片
    label = tk.Label(image=tk_img,bg="white")
    label.image = tk_img  # 保持引用,防止垃圾回收
    label.pack()
def get_total_screen_size():
    monitors = get_monitors()
    width = sum(monitor.width for monitor in monitors)
    height = max(monitor.height for monitor in monitors)
    return width, height

def create_watermark_window(text, **kwargs):

    root = tk.Tk()
    # 获取屏幕分辨率
    screen_width, screen_height = get_total_screen_size()
    def generate_watermark_with_size():
        generate_watermark(text, screen_width, screen_height, **kwargs)

    # 使用 after 方法延迟执行生成水印函数
    root.after(100, generate_watermark_with_size)
    root.overrideredirect(True)
    root.lift()
    root.attributes('-alpha', 0.6)  # 设置透明度
    root.wm_attributes('-topmost', True)
    root.wm_attributes('-disabled', True)
    root.wm_attributes('-transparentcolor',"white")
    hWindow = pywintypes.HANDLE(int(root.frame(), 16))
    exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT
    win32api.SetWindowLong(hWindow, win32con.GWL_EXSTYLE, exStyle)
    root.mainloop()

# 使用示例
if __name__ == '__main__':
    # 获取当前用户名
    username = os.getlogin()
    # 组合文本
    mytext = f"Allianz\n{username}"  # 使用 f-string 格式化字符串
    angle = 45  # 旋转角度
    create_watermark_window(mytext, angle=angle, lines=10)