clock.py
from sys import stdout, stderr, argv
from os import path, O_BINARY
from msvcrt import setmode
from datetime import datetime
from StringIO import StringIO
import math
import Image, ImageDraw, ImageFont
DataPath = 'C:/cjs/www/_dat'
size_border = 25
size_hub = 15
radius_secs = 0.33
radius_mins = 0.60
radius_hrs = 0.75
radius_dots = 0.90
radius_days = 1.05
dt0 = datetime.today()
dt1 = dt0
dims = (400, 300)
bkgd = (255,255,255)
im = Image.new('RGB', dims, bkgd)
draw = ImageDraw.Draw(im)
try:
fx = lambda x: (int(dims[0] / 2) + x)
fy = lambda y: (int(dims[1] / 2) - y)
sz = int((dims[1] - 80) / 2)
draw.rectangle((0,0)+(dims[0]-1,dims[1]-1), outline=( 0, 0, 0))
draw.rectangle((1,1)+(dims[0]-2,dims[1]-2), outline=( 63, 63, 63))
draw.rectangle((2,2)+(dims[0]-3,dims[1]-3), outline=(127,127,127))
draw.rectangle((3,3)+(dims[0]-4,dims[1]-4), outline=(191,191,191))
x1 = size_border
y1 = size_border
x2 = dims[0]-size_border
y2 = dims[1]-size_border
draw.rectangle([x1,y1, x2,y2], fill=(255,255,191), outline=(0,0,0))
fnt = ImageFont.load('C:\\Python27\\Lib\\site-packages\\pilfonts\\helvR10.pil')
draw.text((27,27), "Hello!", font=fnt, fill=(0,0,0))
s = dt1.strftime('%A %B %d, %Y %I:%M %p')
b = draw.textsize(s)
x = 10
y = dims[1] - (b[1]+8)
draw.text((x,y), s, fill=(0,0,0))
s = 'cjsonnack'
b = draw.textsize(s)
x = dims[0] - (b[0]+10)
y = dims[1] - (b[1]+8)
draw.text((x,y), s, fill=(0,0,0))
m = 32
d = int(sz * radius_days)
x1 = fx(-d)
x2 = fx(+d)
y1 = fy(+d)
y2 = fy(-d)
for a in range(m):
a0 = (270 + int(360.0 * (float(a-1)/float(m)))) % 360
a1 = (270 + int(360.0 * (float(a) /float(m)))) % 360
if a == 0:
pass
elif a <= dt1.day:
draw.pieslice([x1,y1, x2,y2], a0,a1, fill=(0,191,255), outline=(0,0,0))
else:
draw.pieslice([x1,y1, x2,y2], a0,a1, outline=(0,0,0))
draw.ellipse((fx(-sz),fy(+sz), fx(+sz),fy(-sz)), fill=(127,191,255), outline=(63,63,63))
d = float(sz * radius_dots)
e = 6
for n,a in enumerate(range(0, 360, 30)):
r = math.radians(60+(360 - a))
x = int(d * math.cos(r))
y = int(d * math.sin(r))
c = (191,63,7) if n < dt1.month else (0,15,63)
x1 = fx(x-e)
x2 = fx(x+e)
y1 = fy(y+e)
y2 = fy(y-e)
draw.ellipse([x1,y1, x2,y2], fill=c, outline=(0,0,0))
d = int(sz * radius_hrs)
t = (270 + int(360.0 * ((dt1.hour % 12) / 12.0))) % 360
x1 = fx(-d)
x2 = fx(+d)
y1 = fy(+d)
y2 = fy(-d)
draw.pieslice([x1,y1, x2,y2], 270,t, fill=(159,127,31), outline=(63,63,0))
d = int(sz * radius_mins)
t = (270 + int(360.0 * (dt1.minute / 60.0))) % 360
x1 = fx(-d)
x2 = fx(+d)
y1 = fy(+d)
y2 = fy(-d)
draw.pieslice([x1,y1, x2,y2], 270,t, fill=(191,255,127), outline=(63,127,31))
d = int(sz * radius_secs)
t = (270 + int(360.0 * (dt1.second / 60.0))) % 360
x1 = fx(-d)
x2 = fx(+d)
y1 = fy(+d)
y2 = fy(-d)
draw.pieslice([x1,y1, x2,y2], 270,t, fill=(255,191,127), outline=(127,63,31))
c = (239,239,63) if dt1.hour < 12 else (63,95,159)
x1 = fx(-size_hub)
x2 = fx(+size_hub)
y1 = fy(+size_hub)
y2 = fy(-size_hub)
draw.ellipse([x1,y1, x2,y2], fill=c, outline=(32,32,32))
except Exception as e:
print >> stderr, e
output = StringIO()
im.save(output, 'PNG')
content = output.getvalue()
output.close()
setmode(stdout.fileno(), O_BINARY)
stdout.write('Content-Type: image/png\n')
stdout.write('Content-Length: %d\n' % len(content))
stdout.write('Last-Modified: %s\n' % dt0.ctime())
stdout.write('Refresh: 1\n')
stdout.write('Pragma: no-cache\n')
stdout.write('Reply-To: Developer@Sonnack.com\n')
stdout.write('Errors-To: Developer@Sonnack.com\n')
stdout.write('My-Dogs-Name: Samantha\n')
stdout.write('\n')
stdout.write(content)
fp = open(path.join(DataPath, 'clock.png'), 'wb')
fp.write(content)
fp.close()