1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import tkinter as tk
from PIL import ImageGrab
import numpy as np
import os
import datetime
import hashlib

# 创建存放图片的目录(如果不存在)
output_dir = "C:/images/"
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# 计算图像内容的哈希值
def calculate_image_hash(image):
    return hashlib.sha256(image.tobytes()).hexdigest()

# 处理按钮点击事件
def save_image_from_clipboard():
    # 使用Pillow库获取剪贴板中的图片数据
    clipboard_image = ImageGrab.grabclipboard()

    if clipboard_image is not None:
        # 计算图像内容的哈希值
        image_hash = calculate_image_hash(clipboard_image)

        # 生成文件名
        current_time = datetime.datetime.now()
        year = str(current_time.year)
        month = str(current_time.month).zfill(2)
        day = str(current_time.day).zfill(2)
        file_name = f"{year}/{month}/{day}/{image_hash}.png"

        # 构建完整的文件路径
        file_path = os.path.join(output_dir, file_name)

        # 创建目录(如果不存在)
        os.makedirs(os.path.dirname(file_path), exist_ok=True)

        # 保存图像文件
        clipboard_image.save(file_path, "PNG")
        result_label.config(text=f"图像已保存到 {file_path}")
    else:
        result_label.config(text="剪贴板中没有图片数据")

# 创建主窗口
root = tk.Tk()
root.title("保存剪贴板图片")

# 创建按钮
save_button = tk.Button(root, text="保存剪贴板图片", command=save_image_from_clipboard)
save_button.pack()

# 创建用于显示结果的标签
result_label = tk.Label(root, text="", wraplength=300)
result_label.pack()

# 运行主循环
root.mainloop()