Using Pillow and Python for generating the WEBP file

I have written now 2-3 API programs in starlark - and it’s not my style. Can’t read or write files to disk and many of the functions are just not as well documented as in Python.

For those who feel similar - install pillow
pip3 install pillow -

and use pillow - e.g.

import os
from PIL import Image, ImageDraw, BdfFontFile, ImageFont

images = []
width = 32
center = 16
center2x = 40

max_radius = int(center * 1.5)
step = 1

#import tidbyt font and convert to pil font
f="/home/pixlet/build/pixlet/fonts/5x8bak.bdf"
fp = open(f, “rb”)
p = BdfFontFile.BdfFontFile(fp)
p.save(f)
font1 = ImageFont.load("/home/pixlet/build/pixlet/fonts/5x8bak.pil")

for i in range(0, max_radius, step):
im = Image.new(‘RGB’, (64, 32), ((i)8, (8-i)8, (i)8))
draw = ImageDraw.Draw(im)
#draw texbox
draw.text((i
2,i), “Licence to”, fill=(255,255,i
7), font=font1)
draw.text((i
2+10,i+10), “Kill”, fill=(255,255,i*7), font=font1)
#draw line
draw.line([(center - i, center - i), (center + i, center + i)], fill=“red”, width = 3 )
#draw circle
draw.ellipse((center2x - i, center - i, center2x + i, center + i), fill=(255,255,255))
images.append(im)

#save webp format
images[0].save(‘pillow1.webp’, ‘webp’,
save_all=True, append_images=images[1:], optimize=False, duration=40, loop=0)
#call shell script to publish using pixlet API
os.system(‘sh /home/pixlet/pubwebp.sh pillow1’)

6 Likes

Love this. Thanks for sharing

I’m struggling to understand why starlark was used for this. I do like the “render.xxx” options. Somewhat easier than pillow in my opinion, but lots of syntactical wierdness and arbitrary limitations otherwise (Ie, can’t use if/then unless it’s in a function, etc)

I would think that once they allow apps to be published (beyond just animated loops being pushed), resources on their servers become a bigger concern, so a constrained language seems to be a better fit than a general one. I’m not a big fan of Python-like languages (Starlark included), but if I have to live in their ecosystem, I have to play by their rules.

1 Like

Ok…. Gotcha. Didn’t think of server load…

Thank you for this, I’m using Pillow to make my first “app” as well. So far, much easier.

After re-writing into Pillow I noticed - the colored text is faint if you don’t use RGBA.
Hence correct my proposal above with

im = Image.new(‘RGBA’, (64, 32), (0,0,0,0))
draw = ImageDraw.Draw(im)
draw.text((3, 22), “Some text” , fill=#0f0f, font=font1)

and for the font I recommend -

f="/home/pixlet/build/pixlet/fonts/tb-8.bdf"

fp = open(f, “rb”)
p = BdfFontFile.BdfFontFile(fp)
p.save(f)
font1 = ImageFont.load("/home/pixlet/build/pixlet/fonts/tb-8.pil")

Still the full blue #00ff is a bit less intense compared to the starlark method.
Not sure how to fix that. All other colors are the same.

Regards
Christoph