Last active 14 hours ago

timmy revised this gist 14 hours ago. Go to revision

1 file changed, 24 insertions, 9 deletions

save_clipboard_image.py

@@ -1,16 +1,31 @@
1 + import os
2 + import pyperclip
3 + from datetime import datetime
1 4 from PIL import ImageGrab
2 5
3 - def save_clipboard_image(filename="screenshot.png"):
4 - # 1. 從剪貼簿抓取影像資料
6 + def 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. 抓取剪貼簿影像
5 12 img = ImageGrab.grabclipboard()
6 13
7 - # 2. 判斷剪貼簿裡是不是影像
8 14 if img is None:
9 - print("剪貼簿裡沒有影像喔!請先截圖再執行。")
10 - else:
11 - # 3. 儲存檔案
12 - img.save(filename, "PNG")
13 - print(f"成功!影像已儲存為:{filename}")
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
14 28
15 29 if __name__ == "__main__":
16 - save_clipboard_image()
30 + result_path = save_and_copy_path()
31 + print(f"路徑已複製:{result_path}")

timmy revised this gist 14 hours ago. Go to revision

1 file changed, 16 insertions

save_clipboard_image.py(file created)

@@ -0,0 +1,16 @@
1 + from PIL import ImageGrab
2 +
3 + def save_clipboard_image(filename="screenshot.png"):
4 + # 1. 從剪貼簿抓取影像資料
5 + img = ImageGrab.grabclipboard()
6 +
7 + # 2. 判斷剪貼簿裡是不是影像
8 + if img is None:
9 + print("剪貼簿裡沒有影像喔!請先截圖再執行。")
10 + else:
11 + # 3. 儲存檔案
12 + img.save(filename, "PNG")
13 + print(f"成功!影像已儲存為:{filename}")
14 +
15 + if __name__ == "__main__":
16 + save_clipboard_image()
Newer Older