|
| 1 | +""" |
| 2 | +Image resizing and drawing using the Pillow Library. For the image, check out the |
| 3 | +associated Adafruit Learn guide at: |
| 4 | +https://learn.adafruit.com/adafruit-eink-display-breakouts/python-code |
| 5 | +
|
| 6 | +Written by Melissa LeBlanc-Williams for Adafruit Industries |
| 7 | +""" |
| 8 | + |
| 9 | +import digitalio |
| 10 | +import busio |
| 11 | +import board |
| 12 | +from PIL import Image, ImageDraw |
| 13 | +from adafruit_epd.epd import Adafruit_EPD |
| 14 | +from adafruit_epd.il0373 import Adafruit_IL0373 |
| 15 | +from adafruit_epd.il91874 import Adafruit_IL91874 # pylint: disable=unused-import |
| 16 | +from adafruit_epd.il0398 import Adafruit_IL0398 # pylint: disable=unused-import |
| 17 | +from adafruit_epd.ssd1608 import Adafruit_SSD1608 # pylint: disable=unused-import |
| 18 | +from adafruit_epd.ssd1675 import Adafruit_SSD1675 # pylint: disable=unused-import |
| 19 | + |
| 20 | + |
| 21 | +# create the spi device and pins we will need |
| 22 | +spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) |
| 23 | +ecs = digitalio.DigitalInOut(board.CE0) |
| 24 | +dc = digitalio.DigitalInOut(board.D22) |
| 25 | +srcs = None |
| 26 | +rst = digitalio.DigitalInOut(board.D27) |
| 27 | +busy = digitalio.DigitalInOut(board.D17) |
| 28 | + |
| 29 | +# give them all to our driver |
| 30 | +#display = Adafruit_SSD1608(200, 200, spi, # 1.54" HD mono display |
| 31 | +#display = Adafruit_SSD1675(122, 250, spi, # 2.13" HD mono display |
| 32 | +#display = Adafruit_IL91874(176, 264, spi, # 2.7" Tri-color display |
| 33 | +#display = Adafruit_IL0373(152, 152, spi, # 1.54" Tri-color display |
| 34 | +#display = Adafruit_IL0373(128, 296, spi, # 2.9" Tri-color display |
| 35 | +#display = Adafruit_IL0398(400, 300, spi, # 4.2" Tri-color display |
| 36 | +display = Adafruit_IL0373(104, 212, spi, # 2.13" Tri-color display |
| 37 | + cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, |
| 38 | + rst_pin=rst, busy_pin=busy) |
| 39 | + |
| 40 | +# IF YOU HAVE A FLEXIBLE DISPLAY (2.13" or 2.9") uncomment these lines! |
| 41 | +#display.set_black_buffer(1, False) |
| 42 | +#display.set_color_buffer(1, False) |
| 43 | + |
| 44 | +display.rotation = 1 |
| 45 | + |
| 46 | +FILENAME = "blinka.png" |
| 47 | + |
| 48 | +width = display.width |
| 49 | +height = display.height |
| 50 | + |
| 51 | +image = Image.new('RGB', (width, height)) |
| 52 | + |
| 53 | +# Get drawing object to draw on image. |
| 54 | +draw = ImageDraw.Draw(image) |
| 55 | + |
| 56 | +image = Image.open(FILENAME) |
| 57 | + |
| 58 | +# Scale the image to the smaller screen dimension |
| 59 | +image_ratio = image.width / image.height |
| 60 | +screen_ratio = width / height |
| 61 | +if screen_ratio < image_ratio: |
| 62 | + scaled_width = image.width * height // image.height |
| 63 | + scaled_height = height |
| 64 | +else: |
| 65 | + scaled_width = width |
| 66 | + scaled_height = image.height * width // image.width |
| 67 | +image = image.resize((scaled_width, scaled_height), Image.BICUBIC) |
| 68 | + |
| 69 | +# Crop and center the image |
| 70 | +x = scaled_width // 2 - width // 2 |
| 71 | +y = scaled_height // 2 - height // 2 |
| 72 | +image = image.crop((x, y, x + width, y + height)) |
| 73 | + |
| 74 | +# Display image. |
| 75 | +display.image(image) |
| 76 | +display.display() |
0 commit comments