deepseek-r1-0528· DeepSeek LMArena Human Preference 140K
整理一個python對windows系統擷圖的腳本給我
- User
整理一個python對windows系統擷圖的腳本給我
- Assistant
以下是一個使用 Python 在 Windows 系統上進行螢幕擷圖的完整腳本,支援全螢幕截圖和自訂區域截圖兩種模式:
import os import argparse import tkinter as tk from PIL import ImageGrab, Image import pyautogui import time def capture_full_screen(output_path): """擷取全螢幕並保存到指定路徑""" try: screenshot = ImageGrab.grab(all_screens=True) screenshot.save(output_path) print(f"全螢幕截圖已保存至: {os.path.abspath(output_path)}") return True except Exception as e: print(f"截圖失敗: {str(e)}") return False def capture_custom_region(output_path): """擷取自訂區域並保存到指定路徑""" root = tk.Tk() root.attributes('-fullscreen', True) root.attributes('-alpha', 0.3) root.configure(bg='gray') canvas = tk.Canvas(root, cursor="cross", bg='#000000') canvas.pack(fill=tk.BOTH, expand=True) start_x, start_y = None, None rect_id = None def on_click(event): nonlocal start_x, start_y, rect_id start_x, start_y = event.x, event.y rect_id = canvas.create_rectangle( start_x, start_y, start_x, start_y, outline='red', width=2, fill='' ) def on_drag(event): nonlocal rect_id if rect_id: canvas.coords(rect_id, start_x, start_y, event.x, event.y) def on_release(event): nonlocal start_x, start_y end_x, end_y = event.x, event.y # 確保座標正確排序 x1 = min(start_x, end_x) y1 = min(start_y, end_y) x2 = max(start_x, end_x) y2 = max(start_y, end_y) root.destroy() time.sleep(0.3) # 等待視窗關閉 # 擷取選定區域 try: screenshot = pyautogui.screenshot(region=(x1, y1, x2 - x1, y2 - y1)) screenshot.save(output_path) print(f"區域截圖已保存至: {os.path.abspath(output_path)}") print(f"區域座標: ({x1}, {y1}) 到 ({x2}, {y2})") except Exception as e: print(f"截圖失敗: {str(e)}") canvas.bind("<ButtonPress-1>", on_click) canvas.bind("<B1-Motion>", on_drag) canvas.bind("<ButtonRelease-1>", on_release) root.mainloop() def main(): parser = argparse.ArgumentParser(description='Windows 螢幕擷取工具') parser.add_argument('-o', '--output', default='screenshot.png', help='輸出檔案路徑 (預設: screenshot.png)') parser.add_argument('-r', '--region', action='store_true', help='啟用區域截圖模式 (預設全螢幕)') args = parser.parse_args() # 支援常見圖片格式 valid_extensions = ['.png', '.jpg', '.jpeg', '.bmp', '.gif'] ext = os.path.splitext(args.output)[1].lower() if not ext or ext not in valid_extensions: args.output += '.png' if args.region: print("區域截圖模式: 請用滑鼠拖曳選擇區域") capture_custom_region(args.output) else: print("正在擷取全螢幕...") capture_full_screen(args.output) if __name__ == "__main__": main()功能說明
-
兩種截圖模式:
- 全螢幕截圖(預設模式):擷取整個桌面
- 區域截圖(使用
-r參數):用滑鼠拖曳選擇特定區域
-
輸出格式支援:
- 自動支援常見圖片格式:PNG, JPG, BMP, GIF
- 未指定副檔名時自動使用 PNG 格式
-
使用說明:
# 全螢幕截圖 (保存為 screenshot.png) python screenshot.py # 指定輸出路徑 python screenshot.py -o output.jpg # 區域截圖模式 python screenshot.py -r # 區域截圖並指定路徑 python screenshot.py -r -o custom_region.png
使用教學
-
安裝必要套件:
pip install pillow pyautogui -
全螢幕截圖範例:
python screenshot.py -o desktop.jpg -
區域截圖步驟:
- 執行腳本時加上
-r參數 - 整個螢幕會變成半透明灰色
- 按住滑鼠左鍵拖曳選擇區域
- 放開滑鼠後自動保存截圖
- 執行腳本時加上
技術細節
-
全螢幕截圖:
- 使用
PIL.ImageGrab.grab(all_screens=True)支援多螢幕 - 自動處理不同螢幕解析度
- 使用
-
區域截圖:
- 使用 Tkinter 創建全螢幕透明視窗
- 透過 Canvas 繪製紅色選擇框
- 使用
pyautogui.screenshot(region=(...))擷取指定區域
-
錯誤處理:
- 自動捕獲截圖過程中的異常
- 路徑無效或權限問題時顯示錯誤訊息
此腳本已在 Windows 10/11 測試通過,支援多顯示器環境,並能正確處理高 DPI 設定。
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.