To demonstrate the usage of the display, we'll initialize it and draw some lines from the Python REPL.
Run the following code to import the necessary modules and set up the pin assignments. We set the SRAM CS pin to None
because the Raspberry Pi has lots of RAM, so we don't really need it.
import digitalio import busio import board from adafruit_epd.epd import Adafruit_EPD spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) ecs = digitalio.DigitalInOut(board.CE0) dc = digitalio.DigitalInOut(board.D22) rst = digitalio.DigitalInOut(board.D27) busy = digitalio.DigitalInOut(board.D17) srcs = None
Run the following code to initialize the older 2.13" 250x122 SSD1675 Monochrome display:
from adafruit_epd.ssd1675 import Adafruit_SSD1675 display = Adafruit_SSD1675(122, 250, spi, cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, rst_pin=rst, busy_pin=busy)
Run the following code to initialize the newer 2.13" 250x122 SSD1680 Monochrome display:
from adafruit_epd.ssd1680 import Adafruit_SSD1680 display = Adafruit_SSD1680(122, 250, spi, cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, rst_pin=rst, busy_pin=busy)
Run the following code to initialize the Flexible 212x104 Monochrome display:
from adafruit_epd.il0373 import Adafruit_IL0373 display = Adafruit_IL0373(104, 212, spi, cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, rst_pin=rst, busy_pin=busy) display.set_black_buffer(1, False) display.set_color_buffer(1, False)
Run the following code to initialize the lower-res Tri-Color display:
from adafruit_epd.il0373 import Adafruit_IL0373 display = Adafruit_IL0373(104, 212, spi, cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, rst_pin=rst, busy_pin=busy)
Run the following code to initialize the newer high-res Tri-Color display:
from adafruit_epd.ssd1680 import Adafruit_SSD1680 display = Adafruit_SSD1680(122, 250, spi, cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, rst_pin=rst, busy_pin=busy)
Monochrome Example
Now we can clear the screens buffer and draw some shapes. Once we're done drawing, we need to tell the screen to update using the display()
method.
display.rotation = 3 display.fill(Adafruit_EPD.WHITE) display.fill_rect(20, 20, 50, 60, Adafruit_EPD.BLACK) display.hline(80, 30, 60, Adafruit_EPD.BLACK) display.vline(80, 30, 60, Adafruit_EPD.BLACK) display.display()
Tri-Color Example
The Tri-Color example is almost the same as the monochrome example, except we added another color in. Once we're done drawing, we need to tell the screen to update using the display()
method.
display.rotation = 3 display.fill(Adafruit_EPD.WHITE) display.fill_rect(20, 20, 50, 60, Adafruit_EPD.RED) display.hline(80, 30, 60, Adafruit_EPD.BLACK) display.vline(80, 30, 60, Adafruit_EPD.BLACK) display.display()
Your display will look something like this:
That's all there is to drawing simple shapes with eInk displays and CircuitPython!
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import digitalio import busio import board from adafruit_epd.epd import Adafruit_EPD from adafruit_epd.il0373 import Adafruit_IL0373 from adafruit_epd.il91874 import Adafruit_IL91874 # pylint: disable=unused-import from adafruit_epd.il0398 import Adafruit_IL0398 # pylint: disable=unused-import from adafruit_epd.ssd1608 import Adafruit_SSD1608 # pylint: disable=unused-import from adafruit_epd.ssd1675 import Adafruit_SSD1675 # pylint: disable=unused-import from adafruit_epd.ssd1680 import Adafruit_SSD1680 # pylint: disable=unused-import from adafruit_epd.ssd1681 import Adafruit_SSD1681 # pylint: disable=unused-import from adafruit_epd.uc8151d import Adafruit_UC8151D # pylint: disable=unused-import from adafruit_epd.ek79686 import Adafruit_EK79686 # pylint: disable=unused-import # create the spi device and pins we will need spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) ecs = digitalio.DigitalInOut(board.D12) dc = digitalio.DigitalInOut(board.D11) srcs = digitalio.DigitalInOut(board.D10) # can be None to use internal memory rst = digitalio.DigitalInOut(board.D9) # can be None to not use this pin busy = digitalio.DigitalInOut(board.D5) # can be None to not use this pin # give them all to our drivers print("Creating display") # display = Adafruit_SSD1608(200, 200, # 1.54" HD mono display # display = Adafruit_SSD1675(122, 250, # 2.13" HD mono display # display = Adafruit_SSD1680(122, 250, # 2.13" HD Tri-color display # display = Adafruit_SSD1681(200, 200, # 1.54" HD Tri-color display # display = Adafruit_IL91874(176, 264, # 2.7" Tri-color display # display = Adafruit_EK79686(176, 264, # 2.7" Tri-color display # display = Adafruit_IL0373(152, 152, # 1.54" Tri-color display # display = Adafruit_UC8151D(128, 296, # 2.9" mono flexible display # display = Adafruit_IL0373(128, 296, # 2.9" Tri-color display # display = Adafruit_IL0398(400, 300, # 4.2" Tri-color display display = Adafruit_IL0373( 104, 212, # 2.13" Tri-color display spi, cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, rst_pin=rst, busy_pin=busy, ) # IF YOU HAVE A 2.13" FLEXIBLE DISPLAY uncomment these lines! # display.set_black_buffer(1, False) # display.set_color_buffer(1, False) # IF YOU HAVE A 2.9" FLEXIBLE DISPLAY uncomment these lines! # display.set_black_buffer(1, True) # display.set_color_buffer(1, True) display.rotation = 1 # clear the buffer print("Clear buffer") display.fill(Adafruit_EPD.WHITE) display.pixel(10, 100, Adafruit_EPD.BLACK) print("Draw Rectangles") display.fill_rect(5, 5, 10, 10, Adafruit_EPD.RED) display.rect(0, 0, 20, 30, Adafruit_EPD.BLACK) print("Draw lines") display.line(0, 0, display.width - 1, display.height - 1, Adafruit_EPD.BLACK) display.line(0, display.height - 1, display.width - 1, 0, Adafruit_EPD.RED) print("Draw text") display.text("hello world", 25, 10, Adafruit_EPD.BLACK) display.display()
Bitmap Example
Here's a complete example of how to display a bitmap image on your display. Note that any .bmp image you want to display must be exactly the size of your display. We will be using the image below on the 1.54" display. Click the button below to download the image and save it as blinka.bmp on your Raspberry Pi. We will be using a Tri-Color bitmap, but it should still work on a monochrome display.
Save the following code to your Raspberry Pi as epd_bitmap.py.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import digitalio import busio import board from adafruit_epd.epd import Adafruit_EPD from adafruit_epd.il0373 import Adafruit_IL0373 from adafruit_epd.il91874 import Adafruit_IL91874 # pylint: disable=unused-import from adafruit_epd.il0398 import Adafruit_IL0398 # pylint: disable=unused-import from adafruit_epd.ssd1608 import Adafruit_SSD1608 # pylint: disable=unused-import from adafruit_epd.ssd1675 import Adafruit_SSD1675 # pylint: disable=unused-import from adafruit_epd.ssd1680 import Adafruit_SSD1680 # pylint: disable=unused-import from adafruit_epd.ssd1681 import Adafruit_SSD1681 # pylint: disable=unused-import from adafruit_epd.uc8151d import Adafruit_UC8151D # pylint: disable=unused-import from adafruit_epd.ek79686 import Adafruit_EK79686 # pylint: disable=unused-import # create the spi device and pins we will need spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) ecs = digitalio.DigitalInOut(board.D10) dc = digitalio.DigitalInOut(board.D9) srcs = digitalio.DigitalInOut(board.D7) # can be None to use internal memory rst = digitalio.DigitalInOut(board.D11) # can be None to not use this pin busy = digitalio.DigitalInOut(board.D12) # can be None to not use this pin # give them all to our driver print("Creating display") # display = Adafruit_SSD1608(200, 200, # 1.54" HD mono display # display = Adafruit_SSD1675(122, 250, # 2.13" HD mono display # display = Adafruit_SSD1680(122, 250, # 2.13" HD Tri-color display # display = Adafruit_SSD1681(200, 200, # 1.54" HD Tri-color display # display = Adafruit_IL91874(176, 264, # 2.7" Tri-color display # display = Adafruit_EK79686(176, 264, # 2.7" Tri-color display # display = Adafruit_IL0373(152, 152, # 1.54" Tri-color display # display = Adafruit_UC8151D(128, 296, # 2.9" mono flexible display # display = Adafruit_IL0373(128, 296, # 2.9" Tri-color display # display = Adafruit_IL0398(400, 300, # 4.2" Tri-color display display = Adafruit_IL0373( 104, 212, # 2.13" Tri-color display spi, cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, rst_pin=rst, busy_pin=busy, ) # IF YOU HAVE A 2.13" FLEXIBLE DISPLAY uncomment these lines! # display.set_black_buffer(1, False) # display.set_color_buffer(1, False) # IF YOU HAVE A 2.9" FLEXIBLE DISPLAY uncomment these lines! # display.set_black_buffer(1, True) # display.set_color_buffer(1, True) display.rotation = 0 FILENAME = "blinka.bmp" def read_le(s): # as of this writting, int.from_bytes does not have LE support, DIY! result = 0 shift = 0 for byte in bytearray(s): result += byte << shift shift += 8 return result class BMPError(Exception): pass def display_bitmap(epd, filename): # pylint: disable=too-many-locals, too-many-branches try: f = open(filename, "rb") # pylint: disable=consider-using-with except OSError: print("Couldn't open file") return print("File opened") try: if f.read(2) != b"BM": # check signature raise BMPError("Not BitMap file") bmpFileSize = read_le(f.read(4)) f.read(4) # Read & ignore creator bytes bmpImageoffset = read_le(f.read(4)) # Start of image data headerSize = read_le(f.read(4)) bmpWidth = read_le(f.read(4)) bmpHeight = read_le(f.read(4)) flip = True print( "Size: %d\nImage offset: %d\nHeader size: %d" % (bmpFileSize, bmpImageoffset, headerSize) ) print("Width: %d\nHeight: %d" % (bmpWidth, bmpHeight)) if read_le(f.read(2)) != 1: raise BMPError("Not singleplane") bmpDepth = read_le(f.read(2)) # bits per pixel print("Bit depth: %d" % (bmpDepth)) if bmpDepth != 24: raise BMPError("Not 24-bit") if read_le(f.read(2)) != 0: raise BMPError("Compressed file") print("Image OK! Drawing...") rowSize = (bmpWidth * 3 + 3) & ~3 # 32-bit line boundary for row in range(bmpHeight): # For each scanline... if flip: # Bitmap is stored bottom-to-top order (normal BMP) pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize else: # Bitmap is stored top-to-bottom pos = bmpImageoffset + row * rowSize # print ("seek to %d" % pos) f.seek(pos) rowdata = f.read(3 * bmpWidth) for col in range(bmpWidth): b, g, r = rowdata[3 * col : 3 * col + 3] # BMP files store RGB in BGR if r < 0x80 and g < 0x80 and b < 0x80: epd.pixel(col, row, Adafruit_EPD.BLACK) elif r >= 0x80 and g >= 0x80 and b >= 0x80: pass # epd.pixel(row, col, Adafruit_EPD.WHITE) elif r >= 0x80: epd.pixel(col, row, Adafruit_EPD.RED) except OSError: print("Couldn't read file") except BMPError as e: print("Failed to parse BMP: " + e.args[0]) finally: f.close() print("Finished drawing") # clear the buffer display.fill(Adafruit_EPD.WHITE) display_bitmap(display, FILENAME) display.display()
Before running it, we need to change a few pin definitions though. Find the section of code that looks like this:
ecs = digitalio.DigitalInOut(board.D10) dc = digitalio.DigitalInOut(board.D9) srcs = digitalio.DigitalInOut(board.D7) # can be None to use internal memory rst = digitalio.DigitalInOut(board.D11) # can be None to not use this pin busy = digitalio.DigitalInOut(board.D12) # can be None to not use this pin
Change the pins to the following to match the wiring on the Raspberry Pi:
ecs = digitalio.DigitalInOut(board.CE0) dc = digitalio.DigitalInOut(board.D22) srcs = None rst = digitalio.DigitalInOut(board.D27) busy = digitalio.DigitalInOut(board.D17)
Next, find the section that looks like this:
# display = Adafruit_SSD1608(200, 200, # 1.54" HD mono display # display = Adafruit_SSD1675(122, 250, # 2.13" HD mono display # display = Adafruit_SSD1680(122, 250, # 2.13" HD Tri-color display # display = Adafruit_SSD1681(200, 200, # 1.54" HD Tri-color display # display = Adafruit_IL91874(176, 264, # 2.7" Tri-color display # display = Adafruit_IL0373(152, 152, # 1.54" Tri-color display # display = Adafruit_IL0373(128, 296, # 2.9" Tri-color display # display = Adafruit_IL0398(400, 300, # 4.2" Tri-color display display = Adafruit_IL0373( 104, 212, # 2.13" Tri-color display spi, cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, rst_pin=rst, busy_pin=busy, )
Comment out these lines:
display = Adafruit_IL0373( 104, 212, # 2.13" Tri-color display
and uncomment the line that corresponds with your display.
Next we tell the display the rotation setting we want to use. This can be a value between 0-3
. For the 2.13" displays, a value of 3
seems to work well.
display.rotation = 3
Now go to the command prompt on your Raspberry Pi and run the script with the following command:
python3 epd_bitmap.py
After a few seconds, your display should show an image like this:
Image Drawing with Pillow
In this image, we will use Pillow to resize and crop the image automatically and draw it the the ePaper Display. Pillow is really powerful and with it you can open and render additional file formats such as PNG or JPG. Let's start with downloading a PNG of blinka that has been adjusted down to 3 colors so it prints nicely on an ePaper Display. We are using PNG for this because it is a lossless format and won't introduce unexpected colors in.
Make sure you save it as blinka.png and place it in the same folder as your script. Here's the code we'll be loading onto the Raspberry Pi. Go ahead and copy it onto your Raspberry Pi and save it as epd_pillow_image.py. We'll go over the interesting parts.
# SPDX-FileCopyrightText: 2019 Melissa LeBlanc-Williams for Adafruit Industries # SPDX-License-Identifier: MIT """ Image resizing and drawing using the Pillow Library. For the image, check out the associated Adafruit Learn guide at: https://learn.adafruit.com/adafruit-eink-display-breakouts/python-code """ import digitalio import busio import board from PIL import Image from adafruit_epd.il0373 import Adafruit_IL0373 from adafruit_epd.il91874 import Adafruit_IL91874 # pylint: disable=unused-import from adafruit_epd.il0398 import Adafruit_IL0398 # pylint: disable=unused-import from adafruit_epd.ssd1608 import Adafruit_SSD1608 # pylint: disable=unused-import from adafruit_epd.ssd1675 import Adafruit_SSD1675 # pylint: disable=unused-import from adafruit_epd.ssd1680 import Adafruit_SSD1680 # pylint: disable=unused-import from adafruit_epd.ssd1681 import Adafruit_SSD1681 # pylint: disable=unused-import from adafruit_epd.uc8151d import Adafruit_UC8151D # pylint: disable=unused-import from adafruit_epd.ek79686 import Adafruit_EK79686 # pylint: disable=unused-import # create the spi device and pins we will need spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) ecs = digitalio.DigitalInOut(board.CE0) dc = digitalio.DigitalInOut(board.D22) srcs = None rst = digitalio.DigitalInOut(board.D27) busy = digitalio.DigitalInOut(board.D17) # give them all to our driver # display = Adafruit_SSD1608(200, 200, # 1.54" HD mono display # display = Adafruit_SSD1675(122, 250, # 2.13" HD mono display # display = Adafruit_SSD1680(122, 250, # 2.13" HD Tri-color or mono display # display = Adafruit_SSD1681(200, 200, # 1.54" HD Tri-color display # display = Adafruit_IL91874(176, 264, # 2.7" Tri-color display # display = Adafruit_EK79686(176, 264, # 2.7" Tri-color display # display = Adafruit_IL0373(152, 152, # 1.54" Tri-color display # display = Adafruit_UC8151D(128, 296, # 2.9" mono flexible display # display = Adafruit_IL0373(128, 296, # 2.9" Tri-color display # display = Adafruit_IL0398(400, 300, # 4.2" Tri-color display display = Adafruit_IL0373( 104, 212, # 2.13" Tri-color display spi, cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, rst_pin=rst, busy_pin=busy, ) # IF YOU HAVE A 2.13" FLEXIBLE DISPLAY uncomment these lines! # display.set_black_buffer(1, False) # display.set_color_buffer(1, False) # IF YOU HAVE A 2.9" FLEXIBLE DISPLAY uncomment these lines! # display.set_black_buffer(1, True) # display.set_color_buffer(1, True) display.rotation = 1 image = Image.open("blinka.png") # Scale the image to the smaller screen dimension image_ratio = image.width / image.height screen_ratio = display.width / display.height if screen_ratio < image_ratio: scaled_width = image.width * display.height // image.height scaled_height = display.height else: scaled_width = display.width scaled_height = image.height * display.width // image.width image = image.resize((scaled_width, scaled_height), Image.BICUBIC) # Crop and center the image x = scaled_width // 2 - display.width // 2 y = scaled_height // 2 - display.height // 2 image = image.crop((x, y, x + display.width, y + display.height)).convert("RGB") # Convert to Monochrome and Add dithering # image = image.convert("1").convert("L") # Display image. display.image(image) display.display()
So we start with our usual imports including a couple of Pillow modules and the ePaper display drivers.
import digitalio import busio import board from PIL import Image, ImageDraw from adafruit_epd.il0373 import Adafruit_IL0373 from adafruit_epd.il91874 import Adafruit_IL91874 from adafruit_epd.il0398 import Adafruit_IL0398 from adafruit_epd.ssd1608 import Adafruit_SSD1608 from adafruit_epd.ssd1675 import Adafruit_SSD1675 from adafruit_epd.ssd1680 import Adafruit_SSD1680 from adafruit_epd.ssd1681 import Adafruit_SSD1681
That is followed by initializing the SPI bus and defining a few pins here. The reason we chose these is because they allow you to use the same code with the EPD bonnets if you chose to do so.
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) ecs = digitalio.DigitalInOut(board.CE0) dc = digitalio.DigitalInOut(board.D22) srcs = None rst = digitalio.DigitalInOut(board.D27) busy = digitalio.DigitalInOut(board.D17)
We wanted to make these examples work on as many displays as possible with very few changes. The older 2.13" Tri-color display is selected by default. For other displays, go ahead and comment out the following lines:
display = Adafruit_IL0373(
104,
212, # 2.13" Tri-color display
and uncomment the line appropriate for your display.
#display = Adafruit_SSD1608(200, 200, # 1.54" HD mono display #display = Adafruit_SSD1675(122, 250, # 2.13" HD mono display #display = Adafruit_SSD1680(122, 250, # 2.13" HD Tri-color display #display = Adafruit_SSD1681(200, 200, # 1.54" HD Tri-color display #display = Adafruit_IL91874(176, 264, # 2.7" Tri-color display #display = Adafruit_IL0373(152, 152, # 1.54" Tri-color display #display = Adafruit_IL0373(128, 296, # 2.9" Tri-color display #display = Adafruit_IL0398(400, 300, # 4.2" Tri-color display display = Adafruit_IL0373( 104, 212, # 2.13" Tri-color display spi, cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, rst_pin=rst, busy_pin=busy )
Next change the rotation setting to 3
.
display.rotation = 3
Next we open the Blinka image, which we've named blinka.png, which assumes it is in the same directory that you are running the script from. Feel free to change it if it doesn't match your configuration.
image = Image.open("blinka.png")
Here's where it starts to get interesting. We want to scale the image so that it matches either the width or height of the display, depending on which is smaller, so that we have some of the image to chop off when we crop it. So we start by calculating the width to height ration of both the display and the image. If the height is the closer of the dimensions, we want to match the image height to the display height and let it be a bit wider than the display. Otherwise, we want to do the opposite.
Once we've figured out how we're going to scale it, we pass in the new dimensions and using a Bicubic rescaling method, we reassign the newly rescaled image back to image
. Pillow has quite a few different methods to choose from, but Bicubic does a great job and is reasonably fast.
Nearest actually gives a little better result with the Tri-color eInks, but loses detail with displaying a color image on the monochrome display, so we decided to go with the best balance.
image_ratio = image.width / image.height screen_ratio = display.width / display.height if screen_ratio < image_ratio: scaled_width = image.width * display.height // image.height scaled_height = display.height else: scaled_width = display.width scaled_height = image.height * display.width // image.width image = image.resize((scaled_width, scaled_height), Image.BICUBIC)
Next we want to figure the starting x and y points of the image where we want to begin cropping it so that it ends up centered. We do that by using a standard centering function, which is basically requesting the difference of the center of the display and the center of the image. Just like with scaling, we replace the image
variable with the newly cropped image.
x = scaled_width // 2 - display.width // 2 y = scaled_height // 2 - display.height // 2 image = image.crop((x, y, x + display.width, y + display.height)).convert("RGB")
Finally, we take our image
, draw it to the frame buffer and display
it. At this point, the image should have the exact same dimensions at the display and fill it completely.
display.image(image) display.display()
Now go to the command prompt on your Raspberry Pi and run the script with the following command:
python3 epd_pillow_image.py
After a few seconds, your display should show this image:
Drawing Shapes and Text with Pillow
In the next example, we'll take a look at drawing shapes and text. This is very similar to the displayio example, but it uses Pillow instead. Go ahead and copy it onto your Raspberry Pi and save it as epd_pillow_demo.py. Here's the code for that.
# SPDX-FileCopyrightText: 2019 Melissa LeBlanc-Williams for Adafruit Industries # SPDX-License-Identifier: MIT """ ePaper Display Shapes and Text demo using the Pillow Library. """ import digitalio import busio import board from PIL import Image, ImageDraw, ImageFont from adafruit_epd.il0373 import Adafruit_IL0373 from adafruit_epd.il91874 import Adafruit_IL91874 # pylint: disable=unused-import from adafruit_epd.il0398 import Adafruit_IL0398 # pylint: disable=unused-import from adafruit_epd.ssd1608 import Adafruit_SSD1608 # pylint: disable=unused-import from adafruit_epd.ssd1675 import Adafruit_SSD1675 # pylint: disable=unused-import from adafruit_epd.ssd1680 import Adafruit_SSD1680 # pylint: disable=unused-import from adafruit_epd.ssd1681 import Adafruit_SSD1681 # pylint: disable=unused-import from adafruit_epd.uc8151d import Adafruit_UC8151D # pylint: disable=unused-import from adafruit_epd.ek79686 import Adafruit_EK79686 # pylint: disable=unused-import # First define some color constants WHITE = (0xFF, 0xFF, 0xFF) BLACK = (0x00, 0x00, 0x00) RED = (0xFF, 0x00, 0x00) # Next define some constants to allow easy resizing of shapes and colors BORDER = 20 FONTSIZE = 24 BACKGROUND_COLOR = BLACK FOREGROUND_COLOR = WHITE TEXT_COLOR = RED # create the spi device and pins we will need spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) ecs = digitalio.DigitalInOut(board.CE0) dc = digitalio.DigitalInOut(board.D22) srcs = None rst = digitalio.DigitalInOut(board.D27) busy = digitalio.DigitalInOut(board.D17) # give them all to our driver # display = Adafruit_SSD1608(200, 200, # 1.54" HD mono display # display = Adafruit_SSD1675(122, 250, # 2.13" HD mono display # display = Adafruit_SSD1680(122, 250, # 2.13" HD Tri-color or mono display # display = Adafruit_SSD1681(200, 200, # 1.54" HD Tri-color display # display = Adafruit_IL91874(176, 264, # 2.7" Tri-color display # display = Adafruit_EK79686(176, 264, # 2.7" Tri-color display # display = Adafruit_IL0373(152, 152, # 1.54" Tri-color display # display = Adafruit_UC8151D(128, 296, # 2.9" mono flexible display # display = Adafruit_IL0373(128, 296, # 2.9" Tri-color display # display = Adafruit_IL0398(400, 300, # 4.2" Tri-color display display = Adafruit_IL0373( 104, 212, # 2.13" Tri-color display spi, cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, rst_pin=rst, busy_pin=busy, ) # IF YOU HAVE A 2.13" FLEXIBLE DISPLAY uncomment these lines! # display.set_black_buffer(1, False) # display.set_color_buffer(1, False) # IF YOU HAVE A 2.9" FLEXIBLE DISPLAY uncomment these lines! # display.set_black_buffer(1, True) # display.set_color_buffer(1, True) display.rotation = 1 image = Image.new("RGB", (display.width, display.height)) # Get drawing object to draw on image. draw = ImageDraw.Draw(image) # Draw a filled box as the background draw.rectangle((0, 0, display.width - 1, display.height - 1), fill=BACKGROUND_COLOR) # Draw a smaller inner foreground rectangle draw.rectangle( (BORDER, BORDER, display.width - BORDER - 1, display.height - BORDER - 1), fill=FOREGROUND_COLOR, ) # Load a TTF Font font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", FONTSIZE) # Draw Some Text text = "Hello World!" (font_width, font_height) = font.getsize(text) draw.text( (display.width // 2 - font_width // 2, display.height // 2 - font_height // 2), text, font=font, fill=TEXT_COLOR, ) # Display image. display.image(image) display.display()
Just like in the last example, we'll do our imports, but this time we're including the ImageDraw
and ImageFont
Pillow modules because we'll be drawing some text this time.
import digitalio import busio import board from PIL import Image, ImageDraw, ImageFont from adafruit_epd.il0373 import Adafruit_IL0373 from adafruit_epd.il91874 import Adafruit_IL91874 from adafruit_epd.il0398 import Adafruit_IL0398 from adafruit_epd.ssd1608 import Adafruit_SSD1608 from adafruit_epd.ssd1675 import Adafruit_SSD1675 from adafruit_epd.ssd1680 import Adafruit_SSD1680 from adafruit_epd.ssd1681 import Adafruit_SSD1681
Next we define some colors that can be used with Pillow.
WHITE = (0xFF, 0xFF, 0xFF) BLACK = (0x00, 0x00, 0x00) RED = (0xFF, 0x00, 0x00)
After that, we create some parameters that are easy to change. If you had a smaller display for instance, you could reduce the FONTSIZE
and BORDER
parameters. The BORDER
will be the size in pixels of the green border between the edge of the display and the inner purple rectangle. The FONTSIZE
will be the size of the font in points so that we can adjust it easily for different displays. You could play around with the colors as well. One thing to note is that on monochrome displays, the RED
will show up as BLACK
.
For the 1.54" display, a BORDER
value of 10 and a FONTSIZE
value of 20 looks good.
BORDER = 10 FONTSIZE = 20 BACKGROUND_COLOR = BLACK FOREGROUND_COLOR = WHITE TEXT_COLOR = RED
After that, the initializer and rotation sections are exactly the same as in the previous example. If you have are using a different display than the 2.13" Tri-color, go ahead and adjust your initializer as explained in the previous example. After that, we will create an image
with our dimensions and use that to create a draw
object. The draw
object will have all of our drawing functions.
image = Image.new('RGB', (display.width, display.height)) draw = ImageDraw.Draw(image)
Next we clear whatever is on the screen by drawing a rectangle using the BACKGROUND_COLOR
that takes up the full screen.
draw.rectangle((0, 0, display.width, display.height), fill=BACKGROUND_COLOR)
Next we will draw an inner rectangle using the FOREGROUND_COLOR
. We use the BORDER
parameter to calculate the size and position that we want to draw the rectangle.
draw.rectangle((BORDER, BORDER, display.width - BORDER - 1, display.height - BORDER - 1), fill=FOREGROUND_COLOR)
Next we'll load a TTF font. The DejaVuSans.ttf
font should come preloaded on your Pi in the location in the code. We also make use of the FONTSIZE
parameter that we discussed earlier.
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', FONTSIZE)
Now we draw the text Hello World onto the center of the display. You may recognize the centering calculation was the same one we used to center crop the image in the previous example. In this example though, we get the font size values using the getsize()
function of the font object.
text = "Hello World!" (font_width, font_height) = font.getsize(text) draw.text((display.width//2 - font_width//2, display.height//2 - font_height//2), text, font=font, fill=TEXT_COLOR)
Finally, just like before, we display the image.
display.image(image) display.display()
Now go to the command prompt on your Raspberry Pi and run the script with the following command:
python3 epd_pillow_demo.py
After a few seconds, your display should show this image:
Text editor powered by tinymce.