LoginSignup
6
6

More than 5 years have passed since last update.

画面キャプチャしてメール送信(ImageGrab)

Last updated at Posted at 2014-02-18
  • Windows機でしか見られない画面を毎日チェックするのって面倒です。またその端末の前に居ないと見れません。その問題を解決するために作ったソースの一部をメモしておきます。
  • 画面キャプチャにはImageGrabを使いました。PILに入っています。Windowsのみ動作の模様。
  • Ubuntu用にはpyscreenshotというものもあるようです。
  • 応用すると、py2exeで作ったexeをサポートして欲しい人に実行してもらい、画面の様子をHTTPでPOST送信ー>メール受信 など組み合わせる事ができます。

画面キャプチャ

画面キャプチャ例
import ImageGrab

def screen_capture(fname):
  img = ImageGrab.grab((150,50,1280,760)) # x,y, x,y
  img.save(fname)

def screen_capture_full(fname):
  img = ImageGrab.grab()
  img.save(fname)

if __name__ == '__main__':
  screen_capture("capture.png")
  screen_capture_fullscreen("capture_fullscreen.png")

画面キャプチャしてメール送信

from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import smtplib

import os
import capture
import time

def ie2png(pages):
    import subprocess
    for page in pages:
      fname = page[0] + ".png"
      ie = "C:\Program Files\Internet Explorer\iexplore.exe"
      p = subprocess.Popen([ie, "-k", page[1]])
      time.sleep(15) # 15秒も待てばレンダリング終わるでしょう。
      capture.screen_capture_full(fname)
      msg.attach(getimage(fname))
      p.kill()

def sendmail(subject, to):
    mailer = smtplib.SMTP()
    mailer.connect("192.168.11.100") # SMTPサーバー
    from_ = "batch@mydmain.com" # 送信者のメールアドレス。任意の値
    msg["subject"] = subject
    mailer.sendmail(from_, to, msg.as_string())
    mailer.close()

if __name__ == '__main__':
  pages = [
      ["googlenews","https://news.google.co.jp"],
      ["cacti","http://myserver/cacti/exported_graph/"],
  ]
  msg = MIMEMultipart()
  ie2png(pages)
  sendmail("news", "mymail@addr.com")

タスクスケジューラに登録。

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