This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
460 lines (397 loc) · 19.8 KB
/
main.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
'''
<INVISOMARK - Greatest Watermark Software>
Copyright (C) <2023> <YuexuChen>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import customtkinter as ctk
import tkinter.filedialog as filedialog
import tkinter.messagebox as messagebox
from PIL import Image, ImageTk
import os
import basic_watermark
import full_watermark
import line
import metadata
import signature
import blind
import blur
import ipfs
import webbrowser
import tracepicture
import tkinter as tk
import tkinter.scrolledtext as scrolledtext
import requests
import re
import io
class CollapsiblePane(ctk.CTkFrame):
def __init__(self, parent, title="", *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.title = title
self.toggle_button = ctk.CTkButton(self, text=self.title, command=self.toggle)
self.toggle_button.pack(fill="x", pady=2)
self.content_frame = ctk.CTkFrame(self)
self.content_visible = False
def toggle(self):
if self.content_visible:
self.content_frame.pack_forget()
self.content_visible = False
else:
self.content_frame.pack(fill="x", expand=True)
self.content_visible = True
def add_widget(self, widget):
widget.pack(fill="x", pady=2)
#必要方法定义
def copy_to_clipboard(text):
app.clipboard_clear()
app.clipboard_append(text)
messagebox.showinfo("复制成功", "已复制到剪贴板")
#更新滑动条
def update_sliders(event=None):
red_value_label.configure(text="红色:"+f"{red_slider.get():.0f}")
green_value_label.configure(text="绿色:"+f"{green_slider.get():.0f}")
blue_value_label.configure(text="蓝色:"+f"{blue_slider.get():.0f}")
alpha_value_label.configure(text="透明度:"+f"{alpha_slider.get() / 10:.1f}")
#选择文件
def choose_file():
file_path = filedialog.askopenfilename()
if file_path:
file_entry.set(file_path)
update_output_path(file_path)
update_image_preview(file_path)
else:
file_entry.set('')
output_entry.set('')
#更新输出文件路径
def update_output_path(input_path):
base, ext = os.path.splitext(input_path)
output_path = base + '_watermarked.png'
output_entry.set(output_path)
#更新实例
def update_image_preview(file_path):
try:
img = Image.open(file_path)
img.thumbnail((200, 200))
img = ImageTk.PhotoImage(img)
image_label.configure(image=img)
image_label.image = img
except Exception as e:
image_label.configure(image='')
image_label.text = '图片预览不可用'
#获取图片路径
def get_working_file():
original = file_entry.get()
watermarked = output_entry.get()
if os.path.exists(original) or os.path.exists(watermarked):
return watermarked if os.path.exists(watermarked) else original
else:
return None
#应用水印
def apply_watermarks():
working_file = get_working_file()
if not working_file:
messagebox.showinfo("提示", "没有选择有效的文件。")
return
output_file = output_entry.get()
try:
#盲水印
if blind_watermark_check.get():
watermark_text = watermark_text_entry.get()
blind_result = blind.embed_watermark(working_file, watermark_text, output_file)
if isinstance(blind_result, tuple):
blind_key = blind_result[0]
blind_key_label.set(f"盲水印密钥(请妥善保管): {blind_key}")
else:
blind_key_label.set(f"盲水印密钥(请妥善保管): {blind_result}")
working_file = output_file
#线性水印
if line_watermark_check.get():
input_text = line_text_entry.get()
font_size = int(font_size_entry.get())
red = int(red_slider.get())
green = int(green_slider.get())
blue = int(blue_slider.get())
alpha = alpha_slider.get() / 10
font_color = (red, green, blue, int(alpha * 255))
line.write_text_along_path(working_file, input_text, output_file, font_size=font_size, font_color=font_color)
#满屏水印
if full_watermark_check.get():
full_watermark_text = full_watermark_text_entry.get()
full_watermark.add_watermark(working_file, output_file, full_watermark_text)
#基础水印
if basic_watermark_check.get():
basic_watermark_text = watermark_text_entry.get()
basic_watermark.add_watermark(working_file, output_file, basic_watermark_text)
#模糊图片
if blur_watermark_check.get():
blur_level = int(blur_level_entry.get())
blur.blur_image(working_file, output_file, blur_level)
#元数据
if metadata_check.get():
artist = artist_entry.get()
software = software_entry.get()
image = Image.open(working_file)
metadata.add_metadata(working_file, {'Artist': artist, 'Software': software})
output_path = os.path.splitext(working_file)[0] + '_watermarked.png'
image.save(output_path, format='PNG')
output_entry.set(output_path)
#数字签名
if signature_check.get():
output_path = os.path.splitext(working_file)[0]+ '.pctf'
key = signature.generate_key_from_string(signature_key_entry.get())
signature.generate_signature(working_file, key, output_path)
#报错输出
except Exception as e:
messagebox.showerror("错误", f"在处理水印时发生错误:{e}")
return
messagebox.showinfo("完成", "水印处理完成。")
def upload_to_ipfs():
file_path = file_entry.get()
if file_path:
result = ipfs.upload_to_ipfs(file_path) # 假设这是您提供的函数
if result.get('ok') and 'value' in result and 'cid' in result['value']:
cid = result['value']['cid']
ipfs_cid.set(f"IPFS CID: {cid}")
ipfs_url.set(f"IPFS链接: https://ipfs.io/ipfs/{cid}")
else:
ipfs_cid.set("上传失败或无效的返回值")
ipfs_url.set("")
else:
messagebox.showinfo("提示", "没有选择有效的文件。")
def extract_watermark_and_metadata():
file_path = get_working_file()
if file_path:
blind_key = blind_key_entry.get()
wm_length = int(blind_key)
extracted_wm = blind.extract_watermark(file_path, wm_length)
extracted_metadata = metadata.read_metadata(file_path)
watermark_result.set(f"提取出的水印: {extracted_wm}")
metadata_result.set(f"元数据: {extracted_metadata}")
else:
messagebox.showinfo("提示", "没有选择有效的文件。")
def verify_signature():
signature_file = filedialog.askopenfilename(filetypes=[("PCTF Files", "*.pctf")])
if signature_file:
picture_path = get_working_file()
key = verify_key_entry
result = signature.verify_signature(picture_path,key,signature_file)
signature_result.set("验证成功" if result else "验证失败")
else:
messagebox.showinfo("提示", "没有选择有效的数字签名文件。")
proxy_url = 'http://node.invisomark.keyfox.xyz/serpapi_search.php'
def fetch_and_display_sources(api_key, image_url, display_window):
try:
page_token = tracepicture.fetch_google_lens_page_token(api_key, image_url)
image_sources_results = tracepicture.fetch_google_lens_image_sources(api_key, page_token)
for source in image_sources_results['image_sources']:
source_frame = tk.Frame(display_window)
source_frame.pack(fill='x', expand=True)
logo = ImageTk.PhotoImage(Image.open(requests.get(source['source_logo'], stream=True).raw))
logo_label = tk.Label(source_frame, image=logo)
logo_label.image = logo
logo_label.pack(side='left')
source_label = tk.Label(source_frame, text=source['source'])
source_label.pack(side='left')
link_button = tk.Button(source_frame, text="打开链接", command=lambda url=source['link']: webbrowser.open(url))
link_button.pack(side='left')
except Exception as e:
messagebox.showerror("错误", f"报错: {e}")
def fetch_and_display_sources_proxy(api_key, image_url, display_window):
response = requests.post(proxy_url, data={'api_key': api_key, 'image_url': image_url})
response_text = response.text
sources = re.findall(r"\[source\] => (.+?)\n", response_text)
source_logos = re.findall(r"\[source_logo\] => (.+?)\n", response_text)
links = re.findall(r"\[link\] => (.+?)\n", response_text)
for source, source_logo, link in zip(sources, source_logos, links):
source_frame = tk.Frame(display_window)
source_frame.pack(fill='x', expand=True)
# 显示 source logo
logo_response = requests.get(source_logo)
logo_image = Image.open(io.BytesIO(logo_response.content))
logo_photo = ImageTk.PhotoImage(logo_image)
logo_label = tk.Label(source_frame, image=logo_photo)
logo_label.image = logo_photo # 保持对图像的引用
logo_label.pack(side='left')
# 显示 source
source_label = tk.Label(source_frame, text=source)
source_label.pack(side='left')
# 显示 link
link_button = tk.Button(source_frame, text="打开链接", command=lambda l=link: webbrowser.open(l))
link_button.pack(side='left')
def trace_image():
api_key = api_key_entry.get()
image_url = image_url_entry.get()
if not api_key:
messagebox.showinfo("提示", "请输入 API 密钥。")
return
if not image_url:
messagebox.showinfo("提示", "请输入图片链接。")
return
try:
display_window = tk.Toplevel(app)
display_window.title("图片回溯结果")
display_window.geometry("600x400")
scrollable_frame = tk.Frame(display_window)
scrollable_canvas = tk.Canvas(scrollable_frame)
scrollbar = tk.Scrollbar(scrollable_frame, orient="vertical", command=scrollable_canvas.yview)
scrollable_frame.pack(fill='both', expand=True)
scrollable_canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
scrollable_canvas.configure(yscrollcommand=scrollbar.set)
scrollable_canvas.bind('<Configure>', lambda e: scrollable_canvas.configure(scrollregion=scrollable_canvas.bbox('all')))
if use_proxy.get():
fetch_and_display_sources_proxy(api_key, image_url, scrollable_canvas)
else:
fetch_and_display_sources(api_key, image_url, scrollable_canvas)
except Exception as e:
messagebox.showerror("错误", f"报错: {e}")
def show_developer_info():
developer_info = "开发者信息:\n开发者:键狐 \n Github: https://github.com/jianhuxwx/invisomark/ \n本项目为免费开源项目,如果付费购买该软件,请立即退款!"
messagebox.showinfo("开发者信息", developer_info)
app = ctk.CTk()
app.geometry("400x600")
app.title("INVISOMARK 键狐制作")
left_frame = ctk.CTkFrame(app, width=200, height=600)
left_frame.pack(side="left", fill="both", expand=True)
right_frame = ctk.CTkFrame(app, width=200, height=600)
right_frame.pack(side="right", fill="both", expand=True)
image_label = ctk.CTkLabel(left_frame, text="图片预览")
image_label.pack(pady=10)
file_entry = ctk.StringVar(app)
output_entry = ctk.StringVar(app)
choose_file_button = ctk.CTkButton(right_frame, text="选择文件", command=choose_file, width=150, height=25)
choose_file_button.pack(pady=5)
file_label = ctk.CTkLabel(right_frame, textvariable=file_entry)
file_label.pack(pady=5)
output_label = ctk.CTkLabel(right_frame, textvariable=output_entry)
output_label.pack(pady=5)
# 盲水印面板
blind_watermark_pane = CollapsiblePane(right_frame, title="盲水印")
blind_watermark_pane.pack(fill="x", pady=2)
blind_watermark_check = ctk.CTkCheckBox(blind_watermark_pane.content_frame, text="启用盲水印")
blind_watermark_pane.add_widget(blind_watermark_check)
watermark_text_entry = ctk.CTkEntry(blind_watermark_pane.content_frame, placeholder_text="水印文本")
blind_watermark_pane.add_widget(watermark_text_entry)
blind_key_label = ctk.StringVar(app)
blind_key_display = ctk.CTkLabel(blind_watermark_pane.content_frame, textvariable=blind_key_label)
blind_watermark_pane.add_widget(blind_key_display)
# 线性水印面板
line_watermark_pane = CollapsiblePane(right_frame, title="线性水印")
line_watermark_pane.pack(fill="x", pady=2)
line_watermark_check = ctk.CTkCheckBox(line_watermark_pane.content_frame, text="启用线性水印")
line_watermark_pane.add_widget(line_watermark_check)
line_text_entry = ctk.CTkEntry(line_watermark_pane.content_frame, placeholder_text="线条水印文本")
line_watermark_pane.add_widget(line_text_entry)
font_size_entry = ctk.CTkEntry(line_watermark_pane.content_frame, placeholder_text="字体大小")
line_watermark_pane.add_widget(font_size_entry)
red_slider = ctk.CTkSlider(line_watermark_pane.content_frame, from_=0, to=255, command=update_sliders)
line_watermark_pane.add_widget(red_slider)
green_slider = ctk.CTkSlider(line_watermark_pane.content_frame, from_=0, to=255, command=update_sliders)
line_watermark_pane.add_widget(green_slider)
blue_slider = ctk.CTkSlider(line_watermark_pane.content_frame, from_=0, to=255, command=update_sliders)
line_watermark_pane.add_widget(blue_slider)
alpha_slider = ctk.CTkSlider(line_watermark_pane.content_frame, from_=0, to=10, command=update_sliders)
line_watermark_pane.add_widget(alpha_slider)
red_value_label = ctk.CTkLabel(line_watermark_pane.content_frame, text="0")
line_watermark_pane.add_widget(red_value_label)
green_value_label = ctk.CTkLabel(line_watermark_pane.content_frame, text="0")
line_watermark_pane.add_widget(green_value_label)
blue_value_label = ctk.CTkLabel(line_watermark_pane.content_frame, text="0")
line_watermark_pane.add_widget(blue_value_label)
alpha_value_label = ctk.CTkLabel(line_watermark_pane.content_frame, text="0.0")
line_watermark_pane.add_widget(alpha_value_label)
# 全屏水印面板
full_watermark_pane = CollapsiblePane(right_frame, title="全屏水印")
full_watermark_pane.pack(fill="x", pady=2)
full_watermark_check = ctk.CTkCheckBox(full_watermark_pane.content_frame, text="启用全屏水印")
full_watermark_pane.add_widget(full_watermark_check)
full_watermark_text_entry = ctk.CTkEntry(full_watermark_pane.content_frame, placeholder_text="全屏水印文本")
full_watermark_pane.add_widget(full_watermark_text_entry)
# 基本水印面板
basic_watermark_pane = CollapsiblePane(right_frame, title="基本水印")
basic_watermark_pane.pack(fill="x", pady=2)
basic_watermark_check = ctk.CTkCheckBox(basic_watermark_pane.content_frame, text="启用基本水印")
basic_watermark_pane.add_widget(basic_watermark_check)
# 模糊水印面板
blur_watermark_pane = CollapsiblePane(right_frame, title="模糊水印")
blur_watermark_pane.pack(fill="x", pady=2)
blur_watermark_check = ctk.CTkCheckBox(blur_watermark_pane.content_frame, text="启用模糊水印")
blur_watermark_pane.add_widget(blur_watermark_check)
blur_level_entry = ctk.CTkEntry(blur_watermark_pane.content_frame, placeholder_text="模糊度")
blur_watermark_pane.add_widget(blur_level_entry)
# 元数据面板
metadata_pane = CollapsiblePane(right_frame, title="元数据")
metadata_pane.pack(fill="x", pady=2)
metadata_check = ctk.CTkCheckBox(metadata_pane.content_frame, text="启用元数据")
metadata_pane.add_widget(metadata_check)
artist_entry = ctk.CTkEntry(metadata_pane.content_frame, placeholder_text="艺术家名称")
metadata_pane.add_widget(artist_entry)
software_entry = ctk.CTkEntry(metadata_pane.content_frame, placeholder_text="软件名称")
metadata_pane.add_widget(software_entry)
# 数字签名面板
signature_pane = CollapsiblePane(right_frame, title="数字签名")
signature_pane.pack(fill="x", pady=2)
signature_check = ctk.CTkCheckBox(signature_pane.content_frame, text="启用数字签名")
signature_pane.add_widget(signature_check)
signature_key_entry = ctk.CTkEntry(signature_pane.content_frame, placeholder_text="数字签名密钥")
signature_pane.add_widget(signature_key_entry)
# 验证面板
verification_pane = CollapsiblePane(right_frame, title="验证")
verification_pane.pack(fill="x", pady=2)
ipfs_cid = ctk.StringVar(app)
ipfs_url = ctk.StringVar(app)
watermark_result = ctk.StringVar(app)
metadata_result = ctk.StringVar(app)
signature_result = ctk.StringVar(app)
upload_to_ipfs_button = ctk.CTkButton(verification_pane.content_frame, text="上传到IPFS", command=upload_to_ipfs, width=150, height=25)
upload_to_ipfs_button.pack(pady=5)
# 在 UI 中添加复制 CID 的按钮
copy_cid_button = ctk.CTkButton(verification_pane.content_frame, text="复制 CID", command=lambda: copy_to_clipboard(ipfs_cid.get()))
copy_cid_button.pack(pady=5)
ipfs_cid_label = ctk.CTkLabel(verification_pane.content_frame, textvariable=ipfs_cid)
# 在 UI 中添加复制 CID 链接的按钮
copy_cid_url_button = ctk.CTkButton(verification_pane.content_frame, text="复制 CID 链接", command=lambda: copy_to_clipboard(ipfs_url.get()))
copy_cid_url_button.pack(pady=5)
ipfs_cid_label.pack(pady=5)
ipfs_url_label = ctk.CTkLabel(verification_pane.content_frame, textvariable=ipfs_url)
ipfs_url_label.pack(pady=5)
blind_key_entry = ctk.CTkEntry(verification_pane.content_frame, placeholder_text="请输入盲水印密钥")
blind_key_entry.pack(pady=5)
extract_wm_button = ctk.CTkButton(verification_pane.content_frame, text="解读水印和元数据", command=extract_watermark_and_metadata, width=150, height=25)
extract_wm_button.pack(pady=5)
watermark_result_label = ctk.CTkLabel(verification_pane.content_frame, textvariable=watermark_result)
watermark_result_label.pack(pady=5)
metadata_result_label = ctk.CTkLabel(verification_pane.content_frame, textvariable=metadata_result)
metadata_result_label.pack(pady=5)
verify_key_entry = ctk.CTkEntry(verification_pane.content_frame, placeholder_text="请输入验证密钥")
verify_key_entry.pack(pady=5)
verify_signature_button = ctk.CTkButton(verification_pane.content_frame, text="验证数字签名", command=verify_signature, width=150, height=25)
verify_signature_button.pack(pady=5)
signature_result_label = ctk.CTkLabel(verification_pane.content_frame, textvariable=signature_result)
signature_result_label.pack(pady=5)
# 图片溯源面板
trace_pane = CollapsiblePane(right_frame, title="图片溯源")
use_proxy = tk.BooleanVar(value=False)
proxy_checkbutton = ctk.CTkCheckBox(trace_pane.content_frame, text="使用代理服务器", variable=use_proxy)
proxy_checkbutton.pack(pady=5)
trace_pane.pack(fill="x", pady=2)
api_key_entry = ctk.CTkEntry(trace_pane.content_frame, placeholder_text="请输入您的API密钥")
api_key_entry.pack(pady=5)
image_url_entry = ctk.CTkEntry(trace_pane.content_frame, placeholder_text="输入图片链接")
image_url_entry.pack(pady=5)
trace_button = ctk.CTkButton(trace_pane.content_frame, text="图片溯源", command=trace_image)
trace_button.pack(pady=5)
developer_info_button = ctk.CTkButton(left_frame, text="显示开发者信息", command=show_developer_info)
developer_info_button.pack(pady=5)
# 应用水印面板
apply_watermarks_button = ctk.CTkButton(right_frame, text="应用水印", command=apply_watermarks, width=150, height=25)
apply_watermarks_button.pack(pady=10)
app.mainloop()