2023年1月22日日曜日

【PyAutoGUI】screenshotによるスクリーンショットの取得

 Pythonによるマウスやキーボード操作を可能にしRPAに役立つライブラリPyAutoGUIのscreenshotにより画面のスクリーンショットを取得できる。

 PyAutoGUIを使うためには以下のようにインポートが必要。

import pyautogui


1. screenshotの使用例

 引数を指定しない場合、全画面のスクリーンショットを取得する。

import pyautogui

im = pyautogui.screenshot()

 スクリーンショットはPillow(PIL)Imageオブジェクト、RGBカラーモードで取得される。

import pyautogui

im = pyautogui.screenshot()

print(type(im))
print(im.size, im.mode)

 実行結果

<class 'PIL.Image.Image'>
(1920, 1080) RGB


 引数にファイル名を指定すると取得したスクリーンショットが画像ファイルとして保存される。

import pyautogui

im = pyautogui.screenshot('screenshot.jpg')


 引数に座標regionを指定すると指定した範囲のみスクリーンショットを取得する。座標は(左上x座標, 左上y座標, 右下x座標, 右下y座標)のタプルで指定する。

import pyautogui

im = pyautogui.screenshot('screenshot.jpg', (0,0, 600, 400))


2. リファレンス

PyAutoGUI > Docs > Screenshot Functions

使用バージョン:Python 3.10.4 / PyAutoGUI 0.9.53

0 件のコメント:

コメントを投稿