Skip to content

Commit 17624ad

Browse files
authored
Merge pull request #53 from FoamyGuy/support_bytesio
BytesIO support
2 parents ffa6224 + 483e72a commit 17624ad

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

adafruit_imageload/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
1818

1919

20-
def load(filename, *, bitmap=None, palette=None):
20+
def load(file_or_filename, *, bitmap=None, palette=None):
2121
"""Load pixel values (indices or colors) into a bitmap and colors into a palette.
2222
2323
bitmap is the desired type. It must take width, height and color_depth in the constructor. It
@@ -39,7 +39,12 @@ def load(filename, *, bitmap=None, palette=None):
3939
# meh, we tried
4040
pass
4141

42-
with open(filename, "rb") as file:
42+
if isinstance(file_or_filename, str):
43+
open_file = open(file_or_filename, "rb")
44+
else:
45+
open_file = file_or_filename
46+
47+
with open_file as file:
4348
header = file.read(3)
4449
file.seek(0)
4550
if header.startswith(b"BM"):

docs/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ Ensure your image loads with this simple test.
66
.. literalinclude:: ../examples/imageload_simpletest.py
77
:caption: examples/imageload_simpletest.py
88
:linenos:
9+
10+
Requests test
11+
-------------
12+
13+
Loads image that is fetched using adafruit_request
14+
15+
.. literalinclude:: ../examples/imageload_from_web.py
16+
:caption: examples/imageload_from_web.py
17+
:linenos:

examples/imageload_from_web.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# SPDX-FileCopyrightText: 2021 Tim C for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
imageload example for esp32s2 that loads an image fetched via
5+
adafruit_requests using BytesIO
6+
"""
7+
from io import BytesIO
8+
import ssl
9+
import wifi
10+
import socketpool
11+
12+
import board
13+
import displayio
14+
import adafruit_requests as requests
15+
import adafruit_imageload
16+
17+
# Get wifi details and more from a secrets.py file
18+
try:
19+
from secrets import secrets
20+
except ImportError:
21+
print("WiFi secrets are kept in secrets.py, please add them there!")
22+
raise
23+
24+
wifi.radio.connect(secrets["ssid"], secrets["password"])
25+
26+
print("My IP address is", wifi.radio.ipv4_address)
27+
28+
socket = socketpool.SocketPool(wifi.radio)
29+
https = requests.Session(socket, ssl.create_default_context())
30+
31+
# pylint: disable=line-too-long
32+
url = "https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_ImageLoad/main/examples/images/4bit.bmp"
33+
34+
print("Fetching text from %s" % url)
35+
response = https.get(url)
36+
print("GET complete")
37+
38+
bytes_img = BytesIO(response.content)
39+
image, palette = adafruit_imageload.load(bytes_img)
40+
tile_grid = displayio.TileGrid(image, pixel_shader=palette)
41+
42+
group = displayio.Group(scale=1)
43+
group.append(tile_grid)
44+
board.DISPLAY.show(group)
45+
46+
response.close()
47+
48+
while True:
49+
pass

0 commit comments

Comments
 (0)