I’m having a hard time with a custom integration using PIL / Pillow that I’m working on. It is supposed to make an image from a weather forecast, and all that works fine. However, I have an issue with getting the right font, so in my devcontainer running on Docker and Windows it looks nice:
Never mind the bad weather or the general looks of it, my main problem is that the font looks terrible. I’ve tried including a font-file and importing it, but it doesn’t seem to accept it.
try:
font = ImageFont.truetype(CONST_FILE_FONT, 25)
except TypeError as e:
_LOGGER.debug(
f"Could not apply font {CONST_FILE_FONT}. Will have to use ugly default font. ({e})"
)
The font file is included in my custom integration in a subdirectory “fonts”. The error it throws at me (in {e} is this: expected string or bytes-like object , but as mentioned it works fine on my dev container.
Ok, so I didn’t solve my issue, but I think I’ve come a bit further.
As stated in the Pillow ImageFont documentation, the truetype method requires an _imagingft service. I wasn’t sure what that is, but after some more googling it seems like is a function included in a libfreetype-dev that may or may not be installed on your server. In case it is installed after Pillow, then Pillow must be removed and re-installed for it to work. Some more info here and here for those interested.
My question can then be re-phrased to: Is it possible to have something like libfreetype-dev installed in Home Assistant? And more specifically when running HassOS? As far as I have understood it is available to install through apk.
Meanwhile, I found a more-or-less workaround by using PIL-fonts as described here and here. Not ideal, but if I’m able to find a font that looks ok enough, I guess it is ok. It should be possible to convert a ttf font into bitmap BDF fonts, but I didn’t get it to work yet. However, I found some BDF-fonts online and then converted it to PIL font a pilfont.py script available with PIL. Not really straight forward, but it works. It would be great to have truetype fonts to work with instead of this cumbersome workaround, so if anyone could help me move forward, I’d be greatful.
Are there good alternatives to Pillow that doesn’t have this issue?
For people that may be interested in this issue in the future, I can also add this guide which uses a tool called FontForge to convert from truetype fonts to bitmap fonts. It works quite well, and I’m able to convert into PIL-fonts with the pilfont.py script.
@tormagj Any chance you could share this component? I just started going down this path for almost the same use case (to show weather station details via a generic camera). A head start would be great if possible.