sharpmemorydisplay_pillow_demo.py
1 """ 2 This demo will fill the screen with white, draw a black box on top 3 and then print Hello World! in the center of the display 4 5 This example is for use on (Linux) computers that are using CPython with 6 Adafruit Blinka to support CircuitPython libraries. CircuitPython does 7 not support PIL/pillow (python imaging library)! 8 """ 9 10 import board 11 import busio 12 import digitalio 13 from PIL import Image, ImageDraw, ImageFont 14 import adafruit_sharpmemorydisplay 15 16 # Colors 17 BLACK = 0 18 WHITE = 255 19 20 # Parameters to Change 21 BORDER = 5 22 FONTSIZE = 10 23 24 spi = busio.SPI(board.SCK, MOSI=board.MOSI) 25 scs = digitalio.DigitalInOut(board.D6) # inverted chip select 26 27 # display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 96, 96) 28 display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 144, 168) 29 30 # Clear display. 31 display.fill(1) 32 display.show() 33 34 # Create blank image for drawing. 35 # Make sure to create image with mode '1' for 1-bit color. 36 image = Image.new("1", (display.width, display.height)) 37 38 # Get drawing object to draw on image. 39 draw = ImageDraw.Draw(image) 40 41 # Draw a black background 42 draw.rectangle((0, 0, display.width, display.height), outline=BLACK, fill=BLACK) 43 44 # Draw a smaller inner rectangle 45 draw.rectangle( 46 (BORDER, BORDER, display.width - BORDER - 1, display.height - BORDER - 1), 47 outline=WHITE, 48 fill=WHITE, 49 ) 50 51 # Load a TTF font. 52 font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", FONTSIZE) 53 54 # Draw Some Text 55 text = "Hello World!" 56 (font_width, font_height) = font.getsize(text) 57 draw.text( 58 (display.width // 2 - font_width // 2, display.height // 2 - font_height // 2), 59 text, 60 font=font, 61 fill=BLACK, 62 ) 63 64 # Display image 65 display.image(image) 66 display.show()