Ultima attività 14 hours ago

save_clipboard_image.py Raw
1import os
2import pyperclip
3from datetime import datetime
4from PIL import ImageGrab
5
6def save_and_copy_path(save_dir="screenshots"):
7 # 1. 建立資料夾
8 if not os.path.exists(save_dir):
9 os.makedirs(save_dir)
10
11 # 2. 抓取剪貼簿影像
12 img = ImageGrab.grabclipboard()
13
14 if img is None:
15 return "剪貼簿沒影像喔!"
16
17 # 3. 產生時間戳記與完整路徑
18 timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
19 full_path = os.path.abspath(os.path.join(save_dir, f"{timestamp}.png"))
20
21 # 4. 儲存影像
22 img.save(full_path, "PNG")
23
24 # 5. 將路徑傳回剪貼簿 (如同 pbcopy)
25 pyperclip.copy(full_path)
26
27 return full_path
28
29if __name__ == "__main__":
30 result_path = save_and_copy_path()
31 print(f"路徑已複製:{result_path}")