Display text on UnicornHAT


Hi there again, is there any way to display text on the UnicornHAT in a similar way to how the Scroll pHAT is capable of working?

My goal is to show the current temperature on the UnicornHAT but so far the easiest way I see this being manageable is to create a .png image file that will display a number and then display this. However, to do this I would need to have something similar to this

temp0 = Image.open('0.png')
temp1 = Image.open('1.png')
temp2 = Image.open('2.png')
temp3 = Image.open('3.png')
temp4 = Image.open('4.png')

then have (I’m certain there’s an easier way of doing this, I just dont know how) if statements that check each temperature value like

  if temp = 4:
      for o_x in range(int(temp4.size[0]/8)):
	for o_y in range(int(temp4.size[1]/8)):

		for x in range(8):
			for y in range(8):
				pixel = temp4.getpixel(((o_x*8)+y,(o_y*8)+x))
				r, g, b = int(pixel[0]),int(pixel[1]),int(pixel[2])
				unicorn.set_pixel(x, y, r, g, b)

My temperature value is obtained from Forecast.io and is then rounded to an integer, I’ve got 120 images from -60 degrees to 60 degrees and writing if statements for these seems like it will become very tedious, is there a way to just pass a value and display this as text on the UnicornHAT?

Thanks for your help!


If you already have an image for each value, you can just put all of them in a dict():

characters = { temp: Image.open('{}.png'.format(temp)) for temp in range(-60, 61) }

Then, the rest of your code should become something like:

character = characters[temp]
width, height = character.size
for o_x in range(width // 8):
    for o_y in range(height // 8):
        for x in range(8):
            for y in range(8):
                r, g, b = character.getpixel((o_x * 8 + y, o_y * 8 + x))
                unicorn.set_pixel(x, y, int(r), int(g), int(b))

Although, the way you are downscaling the initial image to the resolution of the Unicorn is not optimal. ImageMagick has a long guide on its different filters. It would also be best to store the downscaled version so you don’t have to redo the calculation everytime you are displaying the character.

Hope it makes sense…