/ examples / ssd1306_pillow_text.py
ssd1306_pillow_text.py
 1  # This example is for use on (Linux) computers that are using CPython with
 2  # Adafruit Blinka to support CircuitPython libraries. CircuitPython does
 3  # not support PIL/pillow (python imaging library)!
 4  #
 5  # Ported to Pillow by Melissa LeBlanc-Williams for Adafruit Industries from Code available at:
 6  # https://learn.adafruit.com/adafruit-oled-displays-for-raspberry-pi/programming-your-display
 7  
 8  # Imports the necessary libraries...
 9  import board
10  import digitalio
11  from PIL import Image, ImageDraw, ImageFont
12  import adafruit_ssd1306
13  
14  # Setting some variables for our reset pin etc.
15  RESET_PIN = digitalio.DigitalInOut(board.D4)
16  
17  # Very important... This lets py-gaugette 'know' what pins to use in order to reset the display
18  i2c = board.I2C()
19  oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3D, reset=RESET_PIN)
20  
21  # Clear display.
22  oled.fill(0)
23  oled.show()
24  
25  # Create blank image for drawing.
26  image = Image.new("1", (oled.width, oled.height))
27  draw = ImageDraw.Draw(image)
28  
29  # Load a font in 2 different sizes.
30  font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 28)
31  font2 = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 14)
32  
33  # Draw the text
34  draw.text((0, 0), "Hello!", font=font, fill=255)
35  draw.text((0, 30), "Hello!", font=font2, fill=255)
36  draw.text((34, 46), "Hello!", font=font2, fill=255)
37  
38  # Display image
39  oled.image(image)
40  oled.show()