Challenge:
Weather Underground provides relatively long text alerts, however, Indigo has no native facility to display multi-line text. Why let that stop us?
How I Did It:
Enter Pillow. Pillow is the "modern version" of the Python Image Library (PIL). It has the facilities we need to generate images like the one above. The output image has only a couple of components. The underlying image (the black box and the icon) which can be just about anything. The PIL modules let us load the background image, superimpose text onto the image itself, and export it to a file that we can display on a control page. The image above isn't a mock-up, it's the actual output of Pillow that I'm displaying on a control page (okay, it is a bit of a mock-up as we're not going through a heat advisory here thankfully.)
To accomplish this, you will need to install the Pillow Python package. The basic script is simple, and I've added a little bit of flair to select different background images depending on the type of alert, and to dress up the text a bit to make the display more attractive. Here's the script that generated the image above:
- Code: Select all
#! /usr/bin/env python2.6
# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw, ImageFont
import textwrap
alertType = indigo.devices[19099912].states["alertType1"]
textToWrite = indigo.devices[19099912].states["alertMessage1"]
imagePath = '/Library/Application Support/Perceptive Automation/Indigo 6/IndigoWebServer/images/controls/static/'
font = ImageFont.truetype("Arial.ttf",12)
textColor = ("#FFFFFF")
textWidth = 85
writeMargin = 30
writeSpacing = 16
writeSpot = 20
# Load the proper background image based on the alert type.
if alertType in ("FIR"):
image = Image.open(imagePath + "alertFire.png")
elif alertType in ("SEW","WRN"):
image = Image.open(imagePath + "alertLightning.png")
elif alertType in ("WIN"):
image = Image.open(imagePath + "alertSnow.png")
elif alertType in ("FLO","HUR","WAT"):
image = Image.open(imagePath + "alertWater.png")
elif alertType in ("HWW","TOR","WND","TOW"):
image = Image.open(imagePath + "alertWind.png")
else:
image = Image.open(imagePath + "alertBang.png")
textToWrite = textToWrite.replace(u'\u000A', ' ') #replaces [line feed] with [one space]
textToWrite = textToWrite.replace(' ', ' ') #replaces [three adjacent spaces] with [one space]
textToWrite = textToWrite.replace(' ', ' ') #replaces [two adjacent spaces] with [one space]
# Add the text to the image
draw = ImageDraw.Draw(image)
lines = textwrap.wrap(textToWrite, textWidth)
for line in lines:
draw.text((writeMargin, writeSpot), line, font=font, fill=textColor)
writeSpot += writeSpacing
# Write the file.
image.save(imagePath + "alert.png")
The rest is easy. Just add a refreshing image URL to a control page in the usual way. The script can almost certainly be improved with additional functionality, and to make the font display more clearly (although it does look pretty clear on an iPad screen.) You could superimpose the text onto a completely transparent background and make it look like the text was written right on the control page. There are lots of options.
This might be something that I'll add into a future version of the plugin (no promises!) and I will eventually make the alert background images available in the WUnderground Plugin Icon Pack when I'm happy with them. Unfortunately, I can't commit to supporting this as a part of my work on the plugin--just thought I would share. If someone wants to take the concept and turn it into a plugin, be my guest!
The Pillow Package that I used can be found here:
https://www.macports.org/ports.php?by=library&substr=py26-pillow
The instructions for installation and use can be found here:
http://pillow.readthedocs.org/installation.html
http://pillow.readthedocs.org/reference/index.html
Cheers,
Dave
P.S. Of course, this method could be used to display just about any multi-line text in a similar fashion--it works on more than just the weather!