Skip to content

Instantly share code, notes, and snippets.

@Pesticles
Created May 16, 2014 00:16
Show Gist options
  • Save Pesticles/dbf7a2bea6e16a947a89 to your computer and use it in GitHub Desktop.
Save Pesticles/dbf7a2bea6e16a947a89 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import Image, ImageDraw, ImageFont
import sys, os, re
# FName assumed to be in YYYYMMDDHHMMSS.jpg format
fname = sys.argv[1]
assert fname
assert os.path.exists(fname)
temp = sys.argv[2]
humi = sys.argv[3]
baro = sys.argv[4]
font = "/usr/share/fonts/truetype/droid/DroidSerif-Regular.ttf"
timestamp_colour = "yellow"
date = re.match("(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})\d{2}.jpg", fname)
assert date
assert len(date.groups()) == 5
g = date.groups()
timestamp = "%s/%s/%s %s:%s" % (g[2], g[1], g[0], g[3], g[4])
image = Image.open(fname)
draw = ImageDraw.Draw(image)
fontbig = ImageFont.truetype(font, 70)
fontmid = ImageFont.truetype(font, 50)
fontsml = ImageFont.truetype(font, 20)
# Draw timestamp
size = draw.textsize(timestamp, font=fontbig)
across = image.size[0] - size[0] - 10
down = image.size[1] - size[1]
draw.text((across, down), timestamp, font=fontbig, fill=timestamp_colour)
down_value = down - 55
down_label = down_value - 26
# Draw temp
if temp:
temp = temp + u'\N{DEGREE SIGN}' + 'C'
draw.text((across, down_value), temp, font=fontmid, fill='white')
draw.text((across, down_label), 'Temperature:', font=fontsml, fill='white')
# Draw humi
if humi:
humi = humi + '%'
across += 200
draw.text((across, down_value), humi, font=fontmid, fill='white')
draw.text((across, down_label), 'Humidity:', font=fontsml, fill='white')
# Draw baro
if baro:
across += 200
draw.text((across, down_value), baro, font=fontmid, fill='white')
draw.text((across, down_label), 'Pressure:', font=fontsml, fill='white')
# Save
image.save("working/%s" % fname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment