Workaround to get custom fonts on the Piicodev OLED display

This is a workaround I put together to get custom fonts working on the Piicodev OLED module.

IMG_1018

It depends on the imagemagick binaries, so unfortunately it won’t run on microcontrollers (as far as I understand things). You’ll need a TTF font - experiment with one that scales down well.

Install imagemagick

sudo apt install imagemagick

Here’s some code

from PiicoDev_SSD1306 import *
from os import system

display = create_PiicoDev_SSD1306()

def custom_text(text, font, text_size, x, y):
	system('convert -background white -font ' + font  + ' -fill black -pointsize ' + str(text_size) + ' -extent 128x64-' + str(x) + '-' + str(y) + ' label:"' + text + '" -depth 1 +repage custom_text.pbm')
	display.load_pbm('custom_text.pbm', 1)

display.fill(0)
custom_text('Some text', 'myfont.ttf', 24, 1, 1)
display.show()

Couldn’t find a “Python” way to interface with imagemagick, but got around it simply by calling convert through os.system.

Btw, this is the font I used in the picture :slight_smile:

5 Likes

Thanks for sharing.

2 Likes

Hey Steven,

If you’re keen on manipulating images in Python on the Pi 4 I’d check out Pillow as well!

Looks awesome, your experimentation with the OLEDs are great :slight_smile:

2 Likes

Thanks for sharing @Steven :smiley: these code snippets are great!
I’ll index these threads into the OLED tutorials as a going-further section for more experienced makers.

3 Likes

Didn’t even think of using PIL! I’ll have to check if it supports .pbm images.

2 Likes