Skip to content

Instantly share code, notes, and snippets.

@wgbartley
Created July 14, 2014 15:37
Show Gist options
  • Save wgbartley/ea0fc351a88a07f8f250 to your computer and use it in GitHub Desktop.
Save wgbartley/ea0fc351a88a07f8f250 to your computer and use it in GitHub Desktop.
Grab latest webcam picture and display it on a monitor from the command-line
#!/usr/bin/python
import pygame, httplib, io
from pygame.locals import *
from PIL import Image, ImageFont, ImageDraw
from time import time, sleep, strftime, localtime
# Screen setup
print "Press ctrl+c"
display = pygame.display
screen = display.set_mode((640, 480), pygame.FULLSCREEN, 16)
# Hide the mouse cursor
pygame.mouse.set_visible(0)
# How often to refresh image
image_ts = 0
image_refresh = 60
def get_image():
http = httplib.HTTPConnection("etc.ssh22.net")
http.request("GET", "/image/jpeg.cgi")
res = http.getresponse()
data = res.read()
img = Image.open(io.BytesIO(data))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("tahoma.ttf", 32)
text_size = draw.textsize(strftime("%Y-%m-%d %H:%M", localtime()), font)
draw.text((10, (480-20-text_size[1])), strftime("%Y-%m-%d %H:%M", localtime()), font=font, fill=(255, 255, 255))
return pygame.image.fromstring(img.tostring(), (640, 480), "RGB")
while 1:
if(image_ts == 0 or (image_ts+image_refresh)<=time()):
image = get_image()
image_ts = time()
screen.fill((0, 0, 0))
display.flip()
sleep(0.25)
screen.blit(image, (0,0))
display.flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment