rgb_display_pillow_demo.py
1 """ 2 This demo will draw a few rectangles onto the screen along with some text 3 on top of that. 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 Author(s): Melissa LeBlanc-Williams for Adafruit Industries 10 """ 11 12 import digitalio 13 import board 14 from PIL import Image, ImageDraw, ImageFont 15 import adafruit_rgb_display.ili9341 as ili9341 16 import adafruit_rgb_display.st7789 as st7789 # pylint: disable=unused-import 17 import adafruit_rgb_display.hx8357 as hx8357 # pylint: disable=unused-import 18 import adafruit_rgb_display.st7735 as st7735 # pylint: disable=unused-import 19 import adafruit_rgb_display.ssd1351 as ssd1351 # pylint: disable=unused-import 20 import adafruit_rgb_display.ssd1331 as ssd1331 # pylint: disable=unused-import 21 22 # First define some constants to allow easy resizing of shapes. 23 BORDER = 20 24 FONTSIZE = 24 25 26 # Configuration for CS and DC pins (these are PiTFT defaults): 27 cs_pin = digitalio.DigitalInOut(board.CE0) 28 dc_pin = digitalio.DigitalInOut(board.D25) 29 reset_pin = digitalio.DigitalInOut(board.D24) 30 31 # Config for display baudrate (default max is 24mhz): 32 BAUDRATE = 24000000 33 34 # Setup SPI bus using hardware SPI: 35 spi = board.SPI() 36 37 # pylint: disable=line-too-long 38 # Create the display: 39 # disp = st7789.ST7789(spi, rotation=90, # 2.0" ST7789 40 # disp = st7789.ST7789(spi, height=240, y_offset=80, rotation=180, # 1.3", 1.54" ST7789 41 # disp = st7789.ST7789(spi, rotation=90, width=135, height=240, x_offset=53, y_offset=40, # 1.14" ST7789 42 # disp = hx8357.HX8357(spi, rotation=180, # 3.5" HX8357 43 # disp = st7735.ST7735R(spi, rotation=90, # 1.8" ST7735R 44 # disp = st7735.ST7735R(spi, rotation=270, height=128, x_offset=2, y_offset=3, # 1.44" ST7735R 45 # disp = st7735.ST7735R(spi, rotation=90, bgr=True, # 0.96" MiniTFT ST7735R 46 # disp = ssd1351.SSD1351(spi, rotation=180, # 1.5" SSD1351 47 # disp = ssd1351.SSD1351(spi, height=96, y_offset=32, rotation=180, # 1.27" SSD1351 48 # disp = ssd1331.SSD1331(spi, rotation=180, # 0.96" SSD1331 49 disp = ili9341.ILI9341( 50 spi, 51 rotation=90, # 2.2", 2.4", 2.8", 3.2" ILI9341 52 cs=cs_pin, 53 dc=dc_pin, 54 rst=reset_pin, 55 baudrate=BAUDRATE, 56 ) 57 # pylint: enable=line-too-long 58 59 # Create blank image for drawing. 60 # Make sure to create image with mode 'RGB' for full color. 61 if disp.rotation % 180 == 90: 62 height = disp.width # we swap height/width to rotate it to landscape! 63 width = disp.height 64 else: 65 width = disp.width # we swap height/width to rotate it to landscape! 66 height = disp.height 67 68 image = Image.new("RGB", (width, height)) 69 70 # Get drawing object to draw on image. 71 draw = ImageDraw.Draw(image) 72 73 # Draw a green filled box as the background 74 draw.rectangle((0, 0, width, height), fill=(0, 255, 0)) 75 disp.image(image) 76 77 # Draw a smaller inner purple rectangle 78 draw.rectangle( 79 (BORDER, BORDER, width - BORDER - 1, height - BORDER - 1), fill=(170, 0, 136) 80 ) 81 82 # Load a TTF Font 83 font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", FONTSIZE) 84 85 # Draw Some Text 86 text = "Hello World!" 87 (font_width, font_height) = font.getsize(text) 88 draw.text( 89 (width // 2 - font_width // 2, height // 2 - font_height // 2), 90 text, 91 font=font, 92 fill=(255, 255, 0), 93 ) 94 95 # Display image. 96 disp.image(image)