Linux Windows Usage Surveillance in Python

Lets say you want to keep track of a computers usage, for remote working. It should deliver hourly shots of computer usage to an email address that you’ll give it. It will also deliver screen shots every 2 minutes, but you can choose whether it does that or not!

Part 1: Python Screen Capture
Part 2: Python Emailer
Part 3: Setting up the schedule

Part 1: Python Screen Capture

Requirements:

  • Python2.4 or greater
  • Windows or Linux based OS
  • On Windows, you’ll need to get Python Image Library
  • On Linux, you’ll need to have mogrify, which comes as part of ImageMagick.

If you don’t have it use your package manager to get it.

sudo apt-get install imagemagick

The screen module:

#screen.py
import os
import platform
import shutil
from config import launchCommand

def printScreen(filename):
“””prints the screen and saves it as the filename given”””
if platform.system()==’Windows’ or platform.system()==’Microsoft’:
import ImageGrab
img = ImageGrab.grab()
img.save(filename)
else:
launchCommand(‘/usr/bin/xwd -display :0 -root -out %s’%filename)

def convertImage(filename, width=160, height=100):
“””Converts the image found in filename to one of the dimensions given. returns a gif filename”””
if platform.system()==’Windows’ or platform.system()==’Microsoft’:
import Image
im1 = Image.open(filename)
im2 = im1.resize((width, height), Image.NEAREST)
im2.save(‘%s.gif’%filename)
else:
#320×200
launchCommand(‘/usr/bin/mogrify -format gif -resize %sx%s %s’%(width, height, filename))
return ‘%s.gif’%filename

def removeImage(filename):
“””removes the imagename given”””
os.remove(filename)

def copyFile(source,destination):
“””copys a file from source to destination”””
shutil.copyfile(source, destination)

def grabScreen(filename, width=160, height=100):
“””given a filename, width and height, grabs a screen shot of the current desktop,
scales it to the sizes passed in (default 160×100) and saves it to the filename given”””
tmpName = ‘tmp_image’
printScreen(tmpName)
imageName = convertImage(tmpName)
copyFile(imageName, filename)
removeImage(tmpName)
removeImage(imageName)

Leave a Reply

Your email address will not be published. Required fields are marked *