Part3: Setting up the schedule
So you’ve got the python screen capturer and emailer sorted. Now all we need to do is combine the two and set up a process to perform on a regular basis.
1. ScreenMailer
First, we’ll combine the two. When this module is run, it should grab a screen image and send it as is to an email address.
You’ll need to edit a few things. First of all, change the imageName to be in a folder that you don’t mind storing some images.
In my example, I’ve stored it in /tmp (on linux).
The other thing you’ll want to change is your user details.
a) change myEmail to be your email address
b) change userdetails.destination to be the destination email address (this can be the same thing!)
c) change userdetails.username and username.password to have your correct values.
d) if you are not using gmail, find the smtp server address and enter it in serverAddr
#screenMailer.py
import time
import emailer
import screen
def grabScreenAndSend():
imageName = ‘/tmp/images/image%s.gif’%int(time.time())
screen.grabScreen(imageName)
myEmail = ‘xyz@gmail.com’
serverAddr = ‘smtp.gmail.com’
destination = userdetails.destination
timeNow = time.strftime(“%H:%M”, time.localtime())
dayNow = time.strftime(“%a, %d %b %Y”, time.localtime())
emailer.sendEmail(destination, myEmail, ‘My Screen %s’%dayNow,
‘2 Minute Interval Screen Shot, taken at %s. this is just for the filter’%timeNow, serverAddr,
userdetails.username, userdetails.password, [imageName])
if __name__==’__main__’:
grabScreenAndSend()
2. Setting up the scheduler:
a) On linux type crontab -e to edit
add this line
*/2 * * * * python2.4 address_to_your_screen_mailer/screenMailer.py
That will launch the screenMailer every 2 minutes of every day. If you just want the hourly report rather than every two minutes, change the screenMailer.py file so that it does not send the picture. You’ll still need it to run in order to get the hourly report to work
b) on windows use the scheduler, I’ll find out how to do this and edit later on!
3. The hourly summary.
In order to send an hourly summary, we need to create our hourlyScreenSummary.py file.
It takes all of the images found in the folder given and creates a collage and then sends that.
You’ll need to make some more changes for your configuration here.
a) change the OUT_FILE to an appropriate location. this file will get deleted after each run.
b) change the FOLDER to be the same as you set before, where all of the 2 minute pictures were saved
c) change myEmail to be your email address
d) change userdetails.destination to be the destination email address (this can be the same thing!)
e) change userdetails.username and username.password to have your correct values.
f) if you are not using gmail, find the smtp server address and enter it in serverAddr
#hourlyScreenSummary.py
import screenMailer
import emailer
import time
import os
import extras
from config import launchCommand
#combines the whole contents of a specific folder and sends it as one image
OUT_FILE = ‘/tmp/montage.gif’
FOLDER = ‘/tmp/images’
def createMontage(folderaddr):
if platform.system()==’Windows’ or platform.system()==’Microsoft’:
createPILMontage(folderaddr)
else:
createLinuxMontage(folderaddr)
def createLinuxMontage(folderaddr):
files=[]
for filename in os.listdir(folderaddr):
files.append(‘%s/%s’%(folderaddr, filename))
filestring = ‘ ‘.join(files)
launchCommand(‘montage %s -geometry +2-2 %s’%(filestring, OUT_FILE))
def createPILMontage(folderaddr):
import extras
files=[]
for filename in os.listdir(folderaddr):
files.append(‘%s/%s’%(folderaddr, filename))
if len(files):
nocols = 5
norows = int(len(files)/5)+1
newFile = extras.make_contact_sheet(files,(ncols,nrows),(160,100),
(0,0,0,0),2)
newFile.save(OUT_FILE)
def removeImageFiles(folderaddr):
files = []
for filename in os.listdir(folderaddr):
os.remove(‘%s/%s’%(folderaddr, filename))def createAndSendHourlyScreenSummary():
“””create and send an hourly screen summary”””
createMontage(FOLDER)
timeNow = time.strftime(“%H:%M”, time.localtime())
dayNow = time.strftime(“%a, %d %b %Y”, time.localtime())myEmail = ‘xyz@gmail.com’
destination = userdetails.destination
serverAddr = ‘smtp.gmail.com’
emailer.sendEmail(destination, myEmail, ‘My Hourly Screen %s’%dayNow,
‘Hourly Screen Summary, taken at %s this is just for the filter’%timeNow, serverAddr,
userdetails.username, userdetails.password, [OUT_FILE])
#now remove the contents of that file
removeImageFiles(FOLDER)if __name__==’__main__’:
createAndSendHourlyScreenSummary()
4. Add to crontab again
You’ll now need to add this to crontab aswell. Add the following line after typing crontab -e at the command line:
1 * * * * python2.4 (path to hourlyScreenSummary)/hourlyScreenSummary.py
This will send the hourly montage to the address that you have hard coded every hour.
5. And finally
There is one more module that you will need. I found this from http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/v/vi/vidag/montager and have editted it slightly
This module is for the windows version only and is used to create a sheet of screen shots.
#extras.py
import Imagedef make_contact_sheet(fnames,(ncols,nrows),(photow,photoh),
(marl,mart,marr,marb),
padding):
“””\
Make a contact sheet from a group of filenames:fnames A list of names of the image files
ncols Number of columns in the contact sheet
nrows Number of rows in the contact sheet
photow The width of the photo thumbs in pixels
photoh The height of the photo thumbs in pixelsmarl The left margin in pixels
mart The top margin in pixels
marr The right margin in pixels
marl The left margin in pixelspadding The padding between images in pixels
returns a PIL image object.
“””# Read in all images and resize appropriately
imgs = [Image.open(fn).resize((photow,photoh)) for fn in fnames]# Calculate the size of the output image, based on the
# photo thumb sizes, margins, and padding
marw = marl+marr
marh = mart+ marbpadw = (ncols-1)*padding
padh = (nrows-1)*padding
isize = (ncols*photow+marw+padw,nrows*photoh+marh+padh)# Create the new image. The background doesn’t have to be white
white = (255,255,255)
inew = Image.new(‘RGB’,isize,white)# Insert each thumb:
for irow in range(nrows):
for icol in range(ncols):
left = marl + icol*(photow+padding)
right = left + photow
upper = mart + irow*(photoh+padding)
lower = upper + photoh
bbox = (left,upper,right,lower)
try:
img = imgs.pop(0)
except:
break
inew.paste(img,bbox)
return inew
6. Finished
You are finished! You should be able to produce and send images of your desktop to be viewed in your inbox!