Skip to content

BytesIO support #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion adafruit_imageload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the naming here is a bit confusing, because we always have a file in file_or_filename after those lines.
The clean version would be to rename the argument itself to file_or_filename, and then do open_file = open(...) and use open_file everywhere beyond here.

header = file.read(3)
file.seek(0)
if header.startswith(b"BM"):
Expand Down
9 changes: 9 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
49 changes: 49 additions & 0 deletions examples/imageload_from_web.py
Original file line number Diff line number Diff line change
@@ -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