Skip to content

Commit d14e53a

Browse files
authored
Merge pull request #60 from cogliano/master
Update fetch() for portrait oriented images
2 parents 0c06820 + b4d38f3 commit d14e53a

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

adafruit_pyportal.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyPortal.git"
8989

9090
# pylint: disable=line-too-long
91+
# pylint: disable=too-many-lines
9192
# you'll need to pass in an io username, width, height, format (bit depth), io key, and then url!
9293
IMAGE_CONVERTER_SERVICE = "https://io.adafruit.com/api/v2/%s/integrations/image-formatter?x-aio-key=%s&width=%d&height=%d&output=BMP%d&url=%s"
9394
# you'll need to pass in an io username and key
@@ -143,6 +144,8 @@ class PyPortal:
143144
of the width and height you want. Defaults to ``None``.
144145
:param image_position: The position of the image on the display as an (x, y) tuple. Defaults to
145146
``None``.
147+
:param image_dim_json_path: The JSON traversal path for the original dimensions of image tuple.
148+
Used with fetch(). Defaults to ``None``.
146149
:param success_callback: A function we'll call if you like, when we fetch data successfully.
147150
Defaults to ``None``.
148151
:param str caption_text: The text of your caption, a fixed text not changed by the data we get.
@@ -165,7 +168,7 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None,
165168
text_font=None, text_position=None, text_color=0x808080,
166169
text_wrap=False, text_maxlen=0, text_transform=None,
167170
json_transform=None, image_json_path=None,
168-
image_resize=None, image_position=None,
171+
image_resize=None, image_position=None, image_dim_json_path=None,
169172
caption_text=None, caption_font=None, caption_position=None,
170173
caption_color=0x808080, image_url_path=None,
171174
success_callback=None, esp=None, external_spi=None, debug=False):
@@ -368,6 +371,7 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None,
368371
self._image_url_path = image_url_path
369372
self._image_resize = image_resize
370373
self._image_position = image_position
374+
self._image_dim_json_path = image_dim_json_path
371375
if image_json_path or image_url_path:
372376
if self._debug:
373377
print("Init image path")
@@ -838,6 +842,13 @@ def fetch(self, refresh_url=None):
838842
print("Error finding image data. '" + error.args[0] + "' not found.")
839843
self.set_background(self._default_bg)
840844

845+
iwidth = 0
846+
iheight = 0
847+
if self._image_dim_json_path:
848+
iwidth = int(PyPortal._json_traverse(json_out, self._image_dim_json_path[0]))
849+
iheight = int(PyPortal._json_traverse(json_out, self._image_dim_json_path[1]))
850+
print("image dim:", iwidth, iheight)
851+
841852
# we're done with the requests object, lets delete it so we can do more!
842853
json_out = None
843854
r = None
@@ -846,9 +857,16 @@ def fetch(self, refresh_url=None):
846857
if image_url:
847858
try:
848859
print("original URL:", image_url)
849-
image_url = self.image_converter_url(image_url,
850-
self._image_resize[0],
851-
self._image_resize[1])
860+
if iwidth < iheight:
861+
image_url = self.image_converter_url(image_url,
862+
int(self._image_resize[1]
863+
* self._image_resize[1]
864+
/ self._image_resize[0]),
865+
self._image_resize[1])
866+
else:
867+
image_url = self.image_converter_url(image_url,
868+
self._image_resize[0],
869+
self._image_resize[1])
852870
print("convert URL:", image_url)
853871
# convert image to bitmap and cache
854872
#print("**not actually wgetting**")
@@ -865,7 +883,17 @@ def fetch(self, refresh_url=None):
865883
except RuntimeError as error:
866884
print(error)
867885
raise RuntimeError("wget didn't write a complete file")
868-
self.set_background(filename, self._image_position)
886+
if iwidth < iheight:
887+
pwidth = int(self._image_resize[1] *
888+
self._image_resize[1] / self._image_resize[0])
889+
self.set_background(filename,
890+
(self._image_position[0]
891+
+ int((self._image_resize[0]
892+
- pwidth) / 2),
893+
self._image_position[1]))
894+
else:
895+
self.set_background(filename, self._image_position)
896+
869897
except ValueError as error:
870898
print("Error displaying cached image. " + error.args[0])
871899
self.set_background(self._default_bg)

0 commit comments

Comments
 (0)