Skip to content

Instantly share code, notes, and snippets.

@steveway
Created November 9, 2017 15:23
Show Gist options
  • Select an option

  • Save steveway/c9280981599687c3fb05dee215dc6a7b to your computer and use it in GitHub Desktop.

Select an option

Save steveway/c9280981599687c3fb05dee215dc6a7b to your computer and use it in GitHub Desktop.
Create Clock images with separate masks.
# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw, ImageFont, ImageOps
import numpy as np
import os
width = 600
halfwidth = width/2
avatarsize = 48
height = 800
background_color = (255, 255, 255, 0)
background = Image.new ( "RGBA", (width,height), background_color )
# get a font
fnt = ImageFont.truetype('Orbitron-Black.ttf', 100)
draw = ImageDraw.Draw ( background )
for hour in range(24):
for minute in range(60):
text_size = draw.textsize("%02d:%02d"%(hour,minute), fnt)
draw.multiline_text((halfwidth - (text_size[0]/2),150), "%02d:%02d"%(hour,minute), font=fnt, fill=(0,0,0,255), align="center")
print(r"./clock_images/%02d:%02d.png"%(hour,minute))
red, green, blue, alpha = background.split()
background.putalpha(255)
background.save(r"./clock_images/%02d_%02d.png"%(hour,minute), format='png')
alpha.save(r"./clock_masks/%02d_%02d.png"%(hour,minute), format='png')
gmi_image_path = r"./gmi_clock_images/%02d_%02d.gmi"%(hour,minute)
gmi_mask_path = r"./gmi_clock_masks/%02d_%02d.gmi"%(hour,minute)
gmi_image_file = open(gmi_image_path, "wb")
gmi_mask_file = open(gmi_mask_path, "wb")
gmi_image_array_orig = np.asarray(background)
gmi_mask_array_orig = np.asarray(alpha)
gmi_image_array = gmi_image_array_orig.reshape(-1,8)
gmi_image_array = np.fliplr(gmi_image_array)
gmi_image_array = gmi_image_array * 255
gmi_image_array = np.packbits(gmi_image_array,None)
gmi_image_array.tofile(gmi_image_file)
gmi_mask_array = gmi_mask_array_orig.reshape(-1,8)
gmi_mask_array = np.fliplr(gmi_mask_array)
gmi_mask_array = gmi_mask_array * 255
gmi_mask_array = np.packbits(gmi_mask_array,None)
gmi_mask_array.tofile(gmi_mask_file)
gmi_image_file.close()
gmi_mask_file.close()
draw.rectangle((0,0,600,800), background_color)
background.putalpha(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment