0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで一定時間ごとに自動でスクリーンショットを撮影する

0
Posted at

導入

やりたいこと:一定時間ごとに自動でスクリーンショットを撮影したい
...探せばいいアプリがありそうだけどダウンロードとかめんどくさい
→ pythonで作ればいいじゃないか

スクリプト

実行に際して以下の三つの変数をしてください。
保存ファイル名: file_name
キャプチャする時間間隔(秒): captime
キャプチャ枚数: photo_num
下の例では、imgというフォルダに1時間おきに24枚撮影されたスクリーンショットが保存されます。

from PIL import ImageGrab
import datetime
import time
import os
from operator import itemgetter

# 保存ファイル名
file_name = "img"
# キャプチャする時間間隔(秒)
captime = 3600
# キャプチャ枚数
photo_num = 24

# 準備のため実行時に少し間を置く
time.sleep(10)
print('画面キャプチャーを開始します')

# 画像保存先フォルダ
path = './' + file_name + '/'
if not os.path.isdir(path):
    os.makedirs(path)

count = 0

while count < photo_num:
    count = count + 1

    # 画像のキャプチャ&保存
    now = datetime.datetime.now()
    print(f"screen shot:{count}")
    print(now)

    savefilename = file_name + '/screenshot_' + now.strftime('%Y%m%d_%H%M%S') + '.png'
    ImageGrab.grab().save(savefilename)

    time.sleep(captime)

print("finfsh!")

実行

上記のスクリプトをtake_screen.pyとして保存したとします。
windowsで実行すると以下のような表示がされて画像が保存されていきます。

C:\Users\...>python take_screen.py
画面キャプチャーを開始します
screen shot:1
2026-05-29 19:49:35.024549
screen shot:2
2026-05-29 20:49:37.873228
screen shot:3
2026-05-29 22:01:14.862627
...
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?