From 739ed0b68425879201d10e5906d0286c671a01b6 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Mon, 21 Oct 2019 15:34:29 -0700 Subject: [PATCH 1/4] Added Pillow Image drawing example --- examples/epd_pillow_image.py | 76 ++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 examples/epd_pillow_image.py diff --git a/examples/epd_pillow_image.py b/examples/epd_pillow_image.py new file mode 100644 index 0000000..13f41b4 --- /dev/null +++ b/examples/epd_pillow_image.py @@ -0,0 +1,76 @@ +""" +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 + +Written by Melissa LeBlanc-Williams for Adafruit Industries +""" + +import digitalio +import busio +import board +from PIL import Image, ImageDraw +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 + + +# 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, spi, # 1.54" HD mono display +#display = Adafruit_SSD1675(122, 250, spi, # 2.13" HD mono display +#display = Adafruit_IL91874(176, 264, spi, # 2.7" Tri-color display +#display = Adafruit_IL0373(152, 152, spi, # 1.54" Tri-color display +#display = Adafruit_IL0373(128, 296, spi, # 2.9" Tri-color display +#display = Adafruit_IL0398(400, 300, spi, # 4.2" Tri-color display +display = Adafruit_IL0373(104, 212, spi, # 2.13" Tri-color display + cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, + rst_pin=rst, busy_pin=busy) + +# IF YOU HAVE A FLEXIBLE DISPLAY (2.13" or 2.9") uncomment these lines! +#display.set_black_buffer(1, False) +#display.set_color_buffer(1, False) + +display.rotation = 1 + +FILENAME = "blinka.png" + +width = display.width +height = display.height + +image = Image.new('RGB', (width, height)) + +# Get drawing object to draw on image. +draw = ImageDraw.Draw(image) + +image = Image.open(FILENAME) + +# Scale the image to the smaller screen dimension +image_ratio = image.width / image.height +screen_ratio = width / height +if screen_ratio < image_ratio: + scaled_width = image.width * height // image.height + scaled_height = height +else: + scaled_width = width + scaled_height = image.height * width // image.width +image = image.resize((scaled_width, scaled_height), Image.BICUBIC) + +# Crop and center the image +x = scaled_width // 2 - width // 2 +y = scaled_height // 2 - height // 2 +image = image.crop((x, y, x + width, y + height)) + +# Display image. +display.image(image) +display.display() From 7040e6b904fbe8732b2ff6671a00da48556326d5 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Mon, 21 Oct 2019 15:38:52 -0700 Subject: [PATCH 2/4] Removed unused import --- examples/epd_pillow_image.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/epd_pillow_image.py b/examples/epd_pillow_image.py index 13f41b4..e26df9f 100644 --- a/examples/epd_pillow_image.py +++ b/examples/epd_pillow_image.py @@ -10,7 +10,6 @@ import busio import board from PIL import Image, ImageDraw -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 cc338c7b3c82583c532df288411166396088b0cc Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Mon, 21 Oct 2019 16:39:39 -0700 Subject: [PATCH 3/4] Simplified code a bit --- examples/epd_pillow_image.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/examples/epd_pillow_image.py b/examples/epd_pillow_image.py index e26df9f..8c0e3a9 100644 --- a/examples/epd_pillow_image.py +++ b/examples/epd_pillow_image.py @@ -44,10 +44,7 @@ FILENAME = "blinka.png" -width = display.width -height = display.height - -image = Image.new('RGB', (width, height)) +image = Image.new('RGB', (display.width, display.height)) # Get drawing object to draw on image. draw = ImageDraw.Draw(image) @@ -56,19 +53,19 @@ # Scale the image to the smaller screen dimension image_ratio = image.width / image.height -screen_ratio = width / height +screen_ratio = display.width / display.height if screen_ratio < image_ratio: - scaled_width = image.width * height // image.height - scaled_height = height + scaled_width = image.width * display.height // image.height + scaled_height = display.height else: - scaled_width = width - scaled_height = image.height * width // image.width + 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 - width // 2 -y = scaled_height // 2 - height // 2 -image = image.crop((x, y, x + width, y + height)) +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)) # Display image. display.image(image) From 0846fe003d88d8f0770d99de414875dba3fb6470 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Mon, 21 Oct 2019 16:49:02 -0700 Subject: [PATCH 4/4] Removed ImageDraw code which wasn't necessary --- examples/epd_pillow_image.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/examples/epd_pillow_image.py b/examples/epd_pillow_image.py index 8c0e3a9..026d425 100644 --- a/examples/epd_pillow_image.py +++ b/examples/epd_pillow_image.py @@ -9,7 +9,7 @@ import digitalio import busio import board -from PIL import Image, ImageDraw +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 @@ -42,14 +42,7 @@ display.rotation = 1 -FILENAME = "blinka.png" - -image = Image.new('RGB', (display.width, display.height)) - -# Get drawing object to draw on image. -draw = ImageDraw.Draw(image) - -image = Image.open(FILENAME) +image = Image.open("blinka.png") # Scale the image to the smaller screen dimension image_ratio = image.width / image.height