From c53d47b5bcb85b64843422f3551df759c677f1f0 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sat, 12 Jun 2021 12:33:00 -0500 Subject: [PATCH 1/2] BytesIO support --- adafruit_imageload/__init__.py | 6 ++++- docs/examples.rst | 9 +++++++ examples/imageload_from_web.py | 49 ++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 examples/imageload_from_web.py diff --git a/adafruit_imageload/__init__.py b/adafruit_imageload/__init__.py index 8d55ccb..3db14ea 100644 --- a/adafruit_imageload/__init__.py +++ b/adafruit_imageload/__init__.py @@ -39,7 +39,11 @@ def load(filename, *, bitmap=None, palette=None): # meh, we tried pass - with open(filename, "rb") as file: + file_or_filename = filename + if isinstance(file_or_filename, str): + file_or_filename = open(filename, "rb") + + with file_or_filename as file: header = file.read(3) file.seek(0) if header.startswith(b"BM"): diff --git a/docs/examples.rst b/docs/examples.rst index 6390cd8..039d649 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -6,3 +6,12 @@ Ensure your image loads with this simple test. .. literalinclude:: ../examples/imageload_simpletest.py :caption: examples/imageload_simpletest.py :linenos: + +Requests test +------------- + +Loads image that is fetched using adafruit_request + +.. literalinclude:: ../examples/imageload_from_web.py + :caption: examples/imageload_from_web.py + :linenos: diff --git a/examples/imageload_from_web.py b/examples/imageload_from_web.py new file mode 100644 index 0000000..9168cd3 --- /dev/null +++ b/examples/imageload_from_web.py @@ -0,0 +1,49 @@ +# SPDX-FileCopyrightText: 2021 Tim C for Adafruit Industries +# SPDX-License-Identifier: MIT +""" +imageload example for esp32s2 that loads an image fetched via +adafruit_requests using BytesIO +""" +from io import BytesIO +import ssl +import wifi +import socketpool + +import board +import displayio +import adafruit_requests as requests +import adafruit_imageload + +# Get wifi details and more from a secrets.py file +try: + from secrets import secrets +except ImportError: + print("WiFi secrets are kept in secrets.py, please add them there!") + raise + +wifi.radio.connect(secrets["ssid"], secrets["password"]) + +print("My IP address is", wifi.radio.ipv4_address) + +socket = socketpool.SocketPool(wifi.radio) +https = requests.Session(socket, ssl.create_default_context()) + +# pylint: disable=line-too-long +url = "https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_ImageLoad/main/examples/images/4bit.bmp" + +print("Fetching text from %s" % url) +response = https.get(url) +print("GET complete") + +bytes_img = BytesIO(response.content) +image, palette = adafruit_imageload.load(bytes_img) +tile_grid = displayio.TileGrid(image, pixel_shader=palette) + +group = displayio.Group(scale=1) +group.append(tile_grid) +board.DISPLAY.show(group) + +response.close() + +while True: + pass From 483e72ac5681bb2456c993eef94d6cecf4d002a7 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sun, 13 Jun 2021 11:13:45 -0500 Subject: [PATCH 2/2] rename argument --- adafruit_imageload/__init__.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/adafruit_imageload/__init__.py b/adafruit_imageload/__init__.py index 3db14ea..23e7151 100644 --- a/adafruit_imageload/__init__.py +++ b/adafruit_imageload/__init__.py @@ -17,7 +17,7 @@ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git" -def load(filename, *, bitmap=None, palette=None): +def load(file_or_filename, *, bitmap=None, palette=None): """Load pixel values (indices or colors) into a bitmap and colors into a palette. bitmap is the desired type. It must take width, height and color_depth in the constructor. It @@ -39,11 +39,12 @@ def load(filename, *, bitmap=None, palette=None): # meh, we tried pass - file_or_filename = filename if isinstance(file_or_filename, str): - file_or_filename = open(filename, "rb") + open_file = open(file_or_filename, "rb") + else: + open_file = file_or_filename - with file_or_filename as file: + with open_file as file: header = file.read(3) file.seek(0) if header.startswith(b"BM"):