LoginSignup
10
15

More than 3 years have passed since last update.

【Python】スクリーンショットをとる

Last updated at Posted at 2020-02-09

環境

・Mac Catalina
・Python 3.8.1

Macでの設定

そのままだと、スクリーンショットをとってもデスクトップがとられてしまうため、
画面収録の許可を行います。

『システム環境設定』→『セキュリティとプライバシー』

システム環境設定.jpg

『画面収録』にターミナルにチェックを入れる。

画面収録.jpg

スクリーンショットをとる

pyautoguiをインストールします。

pip install pyautogui

pyautogui.screenshot()で全画面のスクリーンショットをとれます。

import pyautogui

screenshot = pyautogui.screenshot()
screenshot.save('スクリーンショット.png')

全体.jpg

範囲を指定する場合

region = (左からの配置位置, 上からの配置位置, 幅, 高さ)を使用することで、
特定範囲のスクリーンショットをとれます。

import pyautogui

screenshot = pyautogui.screenshot(region = (100, 200, 1500, 875))
screenshot.save('スクリーンショット.png')

範囲指定.jpg

JPEGで保存する場合

RGBに変換することで、 JPEGで保存することができます。

screenshot = screenshot.convert('RGB')

参考

容量の大きいpngファイルを、jpgファイルに変換する
pyautogui

10
15
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
15