/ examples / rgb_display_pillow_image.py
rgb_display_pillow_image.py
 1  """
 2  Be sure to check the learn guides for more usage information.
 3  
 4  This example is for use on (Linux) computers that are using CPython with
 5  Adafruit Blinka to support CircuitPython libraries. CircuitPython does
 6  not support PIL/pillow (python imaging library)!
 7  
 8  Author(s): Melissa LeBlanc-Williams for Adafruit Industries
 9  """
10  
11  import digitalio
12  import board
13  from PIL import Image, ImageDraw
14  import adafruit_rgb_display.ili9341 as ili9341
15  import adafruit_rgb_display.st7789 as st7789  # pylint: disable=unused-import
16  import adafruit_rgb_display.hx8357 as hx8357  # pylint: disable=unused-import
17  import adafruit_rgb_display.st7735 as st7735  # pylint: disable=unused-import
18  import adafruit_rgb_display.ssd1351 as ssd1351  # pylint: disable=unused-import
19  import adafruit_rgb_display.ssd1331 as ssd1331  # pylint: disable=unused-import
20  
21  # Configuration for CS and DC pins (these are PiTFT defaults):
22  cs_pin = digitalio.DigitalInOut(board.CE0)
23  dc_pin = digitalio.DigitalInOut(board.D25)
24  reset_pin = digitalio.DigitalInOut(board.D24)
25  
26  # Config for display baudrate (default max is 24mhz):
27  BAUDRATE = 24000000
28  
29  # Setup SPI bus using hardware SPI:
30  spi = board.SPI()
31  
32  # pylint: disable=line-too-long
33  # Create the display:
34  # disp = st7789.ST7789(spi, rotation=90,                            # 2.0" ST7789
35  # disp = st7789.ST7789(spi, height=240, y_offset=80, rotation=180,  # 1.3", 1.54" ST7789
36  # disp = st7789.ST7789(spi, rotation=90, width=135, height=240, x_offset=53, y_offset=40, # 1.14" ST7789
37  # disp = hx8357.HX8357(spi, rotation=180,                           # 3.5" HX8357
38  # disp = st7735.ST7735R(spi, rotation=90,                           # 1.8" ST7735R
39  # disp = st7735.ST7735R(spi, rotation=270, height=128, x_offset=2, y_offset=3,   # 1.44" ST7735R
40  # disp = st7735.ST7735R(spi, rotation=90, bgr=True,                 # 0.96" MiniTFT ST7735R
41  # disp = ssd1351.SSD1351(spi, rotation=180,                         # 1.5" SSD1351
42  # disp = ssd1351.SSD1351(spi, height=96, y_offset=32, rotation=180, # 1.27" SSD1351
43  # disp = ssd1331.SSD1331(spi, rotation=180,                         # 0.96" SSD1331
44  disp = ili9341.ILI9341(
45      spi,
46      rotation=90,  # 2.2", 2.4", 2.8", 3.2" ILI9341
47      cs=cs_pin,
48      dc=dc_pin,
49      rst=reset_pin,
50      baudrate=BAUDRATE,
51  )
52  # pylint: enable=line-too-long
53  
54  # Create blank image for drawing.
55  # Make sure to create image with mode 'RGB' for full color.
56  if disp.rotation % 180 == 90:
57      height = disp.width  # we swap height/width to rotate it to landscape!
58      width = disp.height
59  else:
60      width = disp.width  # we swap height/width to rotate it to landscape!
61      height = disp.height
62  image = Image.new("RGB", (width, height))
63  
64  # Get drawing object to draw on image.
65  draw = ImageDraw.Draw(image)
66  
67  # Draw a black filled box to clear the image.
68  draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
69  disp.image(image)
70  
71  image = Image.open("blinka.jpg")
72  
73  # Scale the image to the smaller screen dimension
74  image_ratio = image.width / image.height
75  screen_ratio = width / height
76  if screen_ratio < image_ratio:
77      scaled_width = image.width * height // image.height
78      scaled_height = height
79  else:
80      scaled_width = width
81      scaled_height = image.height * width // image.width
82  image = image.resize((scaled_width, scaled_height), Image.BICUBIC)
83  
84  # Crop and center the image
85  x = scaled_width // 2 - width // 2
86  y = scaled_height // 2 - height // 2
87  image = image.crop((x, y, x + width, y + height))
88  
89  # Display image.
90  disp.image(image)