使用 Python 程式設計擷取螢幕截圖



在某些情況下,例如自動化測試或製作故障紀錄片和教學內容,程式化截圖方法可能很有用。作為一門動態語言,Python 擁有多個使用者友好的庫,例如 pytest;在這些庫中,pyscreenshot 被認為是最簡單、最快速的庫之一。使用此工具,可以捕獲整個螢幕或螢幕的一部分。

安裝

在開始編寫程式碼之前,需要安裝 pyscreenshot 庫。這可以透過 pip 輕鬆完成。

語法

pip install pyscreenshot

此外,請確保在系統中安裝了 Python 庫 pillow,它將幫助您獲得影像處理功能。

語法

pip install pillow

基本截圖

擷取螢幕截圖最簡單的方法是捕獲整個螢幕。ImageGrab.grab() 捕獲整個螢幕並將其作為影像物件返回。screenshot.show() 在預設的影像檢視器中開啟捕獲的螢幕截圖。

示例

import pyscreenshot as ImageGrab

# Capture full screen
screenshot = ImageGrab.grab()

# Save the screenshot to a file 
screenshot.save('screenshot.png') # You can change the file name and extension as needed
# Display the screenshot
screenshot.show()

輸出

螢幕截圖將在預設的影像檢視器中顯示。

Screenshot

捕獲特定區域

有時,您只需要螢幕的一部分,而不是整個部分。在這種情況下,您可以指定捕獲特定區域。bbox 是一個元組,定義了要捕獲的區域,而 ImageGrab.grab(bbox=bbox) 則捕獲指定的區域。

示例

import pyscreenshot as ImageGrab

# Define the bounding box (left_x, top_y, right_x, bottom_y)
bbox = (10, 10, 500, 500)

# Capture a specific region
screenshot = ImageGrab.grab(bbox=bbox)

# Display the screenshot
screenshot.show()

輸出

定義的區域將在預設的影像檢視器中顯示。

Screenshot

儲存螢幕截圖

要將捕獲的螢幕截圖儲存到檔案,請使用 save 方法。screenshot.save('screenshot.png') 將螢幕截圖儲存到名為 screenshot.png 的檔案中。

示例

import pyscreenshot as ImageGrab

# Capture full screen
screenshot = ImageGrab.grab()

# Save the screenshot to a file
screenshot.save('screenshot.png')

輸出

螢幕截圖將儲存為當前工作目錄中的 screenshot.png。

在擷取螢幕截圖之前新增延遲

在捕獲螢幕截圖之前引入延遲在各種場景中都很有用。time.sleep(5) 在擷取螢幕截圖之前暫停執行 5 秒。

示例

import pyscreenshot as ImageGrab
import time

# Delay for 5 seconds
time.sleep(5)

# Capture full screen
screenshot = ImageGrab.grab()

# Display the screenshot
screenshot.show()

輸出

螢幕截圖將在延遲 5 秒後捕獲,並在預設的影像檢視器中顯示。

連續擷取多個螢幕截圖

您可能需要快速連續捕獲多個螢幕截圖。迴圈捕獲五個螢幕截圖,每個螢幕截圖都使用唯一的檔名儲存。time.sleep(2) 在每次捕獲之間引入 2 秒的延遲。

示例

import pyscreenshot as ImageGrab
import time

for i in range(5):
   # Capture full screen
   screenshot = ImageGrab.grab()

   # Save the screenshot to a file
   screenshot.save(f'screenshot_{i}.png')

   # Delay for 2 seconds
   time.sleep(2)

輸出

五個螢幕截圖將儲存為 screenshot_0.png、screenshot_1.png、screenshot_2.png、screenshot_3.png 和 screenshot_4.png。

請注意,這將建立多個影像螢幕截圖。

Screenshot

在螢幕截圖中新增白色文字

有時,您可能希望在螢幕截圖中新增文字以進行註釋或標記。以下是使用 Python 中的 Pillow 庫將白色文字新增到螢幕截圖的示例。此示例捕獲螢幕截圖,向其新增白色文字,然後儲存並顯示修改後的影像。

安裝

在開始編寫程式碼之前,您必須安裝 pillow 庫。這可以使用 pip 輕鬆完成。

pip install pillow

示例

from PIL import Image, ImageDraw, ImageFont
import pyscreenshot as ImageGrab

# Define the text to add
x = "Hello, World!"

try:
   # Capture the screenshot
   screenshot = ImageGrab.grab()

   # Create an ImageDraw object
   draw = ImageDraw.Draw(screenshot)

   # Load a font
   try:
      # Use a truetype font (you can download a .ttf file and specify the path here)
      font = ImageFont.truetype("arial.ttf", size=50)
   except IOError:
      # Fallback to default font if the specified font is not available
      font = ImageFont.load_default()

   # Define the position and color for the text
   text_position = (50, 50)  # Adjusted position
   text_color = "white"  # Change text color to white

   # Add text to the screenshot
   draw.text(text_position, x, font=font, fill=text_color)

   # Save the screenshot with text
   screenshot.save('screenshot_with_text.png')

   # Display the screenshot
   screenshot.show()

   print("Screenshot captured and saved successfully.")

except Exception as e:
   print(f"An error occurred: {e}")

輸出

Screenshot

結論

在本指南中,我們向您展示了使用 Python 中的 **pyscreenshot** 捕獲螢幕截圖的各種技術。從基本的全屏捕獲到目標特定區域、儲存檔案、新增延遲以及連續拍攝多張螢幕截圖,**pyscreenshot** 被證明是自動化和教學內容建立的通用工具。

python_projects_from_basic_to_advanced.htm
廣告