Skip to content

Instantly share code, notes, and snippets.

@rgov
Created October 8, 2018 20:52
Show Gist options
  • Save rgov/37eb19052098e4482388f77705e85ff2 to your computer and use it in GitHub Desktop.
Save rgov/37eb19052098e4482388f77705e85ff2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
'''
These are some helper routines I wrote for drawing images for a dual-color
128x64 OLED display. The display has a 16-row strip of cyan pixels along the
top, followed by an unaddressable strip of black which is 2 pixels in height,
then the remaining 48 rows are yellow.
https://www.amazon.com/gp/product/B00O2LLT30/
To make it easier to keep drawings in the correct aspect ratio, I preferred to
just draw a 128x66 image and then slice out the black strip before outputting
to the display. The `prepare()` function does this for you.
If you would like to see a rendering of what the image will look like with the
display's colors, use `render()`.
You can use the Adafruit_SSD1306 module to actually send the image to the
display.
https://github.com/adafruit/Adafruit_Python_SSD1306
'''
from PIL import Image, ImageDraw, ImageFont
WHITE = 255
YELLOW = (246, 236, 110)
CYAN = (150, 236, 255)
WIDTH = 128
HEIGHT = 64
GAP_Y = 16
GAP_HEIGHT = 2
def demo_image():
# Adapted from the Adafruit 'shapes.py' sample code by Tony DiCola
image = Image.new('1', (WIDTH, HEIGHT+GAP_HEIGHT))
draw = ImageDraw.Draw(image)
# First define some constants to allow easy resizing of shapes.
padding = 2
shape_width = 20
top = padding
bottom = HEIGHT+GAP_HEIGHT-padding
x = padding
# Draw an ellipse
draw.ellipse((x, top , x+shape_width, bottom), outline=WHITE)
x += shape_width+padding
# Draw a rectangle
draw.rectangle((x, top, x+shape_width, bottom), outline=WHITE)
x += shape_width+padding
# Draw a triangle
draw.polygon([(x, bottom), (x+shape_width/2, top), (x+shape_width, bottom)],
outline=WHITE)
x += shape_width+padding
# Draw an X
draw.line((x, bottom, x+shape_width, top), fill=WHITE)
draw.line((x, top, x+shape_width, bottom), fill=WHITE)
x += shape_width+padding
# Write some text
font = ImageFont.load_default()
draw.text((x, top), 'Hello', font=font, fill=WHITE)
draw.text((x, top+20), 'World!', font=font, fill=WHITE)
return image
def create_bg():
bg = Image.new('RGB', (WIDTH, HEIGHT+GAP_HEIGHT))
draw = ImageDraw.Draw(bg)
draw.rectangle((0, 0, WIDTH, GAP_Y-1), fill=YELLOW)
draw.rectangle((0, GAP_Y+GAP_HEIGHT, WIDTH, HEIGHT+GAP_HEIGHT), fill=CYAN)
return bg
def render(image):
rendered = Image.new('RGB', (WIDTH, HEIGHT+GAP_HEIGHT))
rendered.paste(create_bg(), image)
return rendered
def prepare(image):
output = Image.new('1', (WIDTH, HEIGHT))
draw = ImageDraw.Draw(output)
draw.rectangle((0, 0, WIDTH, HEIGHT), fill=WHITE)
output.paste(image.crop((0, 0, WIDTH, GAP_Y)), (0, 0))
output.paste(image.crop((0, GAP_Y+GAP_HEIGHT, WIDTH, HEIGHT+GAP_HEIGHT)),
(0, GAP_Y))
return output
if __name__ == '__main__':
render(demo_image()).show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment