From 04927c76fa8fae6a810c32dd3297d0bfa7c09119 Mon Sep 17 00:00:00 2001 From: Dan Cogliano Date: Mon, 6 Jan 2020 13:54:29 -0500 Subject: [PATCH 1/5] Update fetch() for portrait oriented images --- adafruit_pyportal.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index 510b549..86f0d4b 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -143,6 +143,8 @@ class PyPortal: of the width and height you want. Defaults to ``None``. :param image_position: The position of the image on the display as an (x, y) tuple. Defaults to ``None``. + :param image_dim_json_path: The JSON traversal path for the original dimensions of image tuple. + Used with fetch(). Defaults to ``None``. :param success_callback: A function we'll call if you like, when we fetch data successfully. Defaults to ``None``. :param str caption_text: The text of your caption, a fixed text not changed by the data we get. @@ -165,7 +167,7 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None, text_font=None, text_position=None, text_color=0x808080, text_wrap=False, text_maxlen=0, text_transform=None, json_transform=None, image_json_path=None, - image_resize=None, image_position=None, + image_resize=None, image_position=None, image_dim_json_path=None, caption_text=None, caption_font=None, caption_position=None, caption_color=0x808080, image_url_path=None, success_callback=None, esp=None, external_spi=None, debug=False): @@ -368,6 +370,7 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None, self._image_url_path = image_url_path self._image_resize = image_resize self._image_position = image_position + self._image_dim_json_path = image_dim_json_path if image_json_path or image_url_path: if self._debug: print("Init image path") @@ -838,6 +841,13 @@ def fetch(self, refresh_url=None): print("Error finding image data. '" + error.args[0] + "' not found.") self.set_background(self._default_bg) + iwidth = 0 + iheight = 0 + if self._image_dim_json_path: + iwidth = int(PyPortal._json_traverse(json_out, self._image_dim_json_path[0])) + iheight = int(PyPortal._json_traverse(json_out, self._image_dim_json_path[1])) + print("image dim:",iwidth,iheight) + # we're done with the requests object, lets delete it so we can do more! json_out = None r = None @@ -846,9 +856,15 @@ def fetch(self, refresh_url=None): if image_url: try: print("original URL:", image_url) - image_url = self.image_converter_url(image_url, - self._image_resize[0], - self._image_resize[1]) + if iwidth < iheight: + image_url = self.image_converter_url(image_url, + int( self._image_resize[1] * self._image_resize[1] + / self._image_resize[0]), + self._image_resize[1]) + else: + image_url = self.image_converter_url(image_url, + self._image_resize[0], + self._image_resize[1]) print("convert URL:", image_url) # convert image to bitmap and cache #print("**not actually wgetting**") @@ -865,7 +881,13 @@ def fetch(self, refresh_url=None): except RuntimeError as error: print(error) raise RuntimeError("wget didn't write a complete file") - self.set_background(filename, self._image_position) + if iwidth < iheight: + pwidth = int( self._image_resize[1] * self._image_resize[1] / self._image_resize[0]) + self.set_background(filename, (self._image_position[0] + int((self._image_resize[0] - pwidth) / 2), + self._image_position[1])) + else: + self.set_background(filename, self._image_position) + except ValueError as error: print("Error displaying cached image. " + error.args[0]) self.set_background(self._default_bg) From bae19bbaae074322e47ff364194cd7d4311feffa Mon Sep 17 00:00:00 2001 From: Dan Cogliano Date: Mon, 6 Jan 2020 14:36:54 -0500 Subject: [PATCH 2/5] Fixed some spacing issues --- adafruit_pyportal.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index 86f0d4b..6c66fbe 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -846,7 +846,7 @@ def fetch(self, refresh_url=None): if self._image_dim_json_path: iwidth = int(PyPortal._json_traverse(json_out, self._image_dim_json_path[0])) iheight = int(PyPortal._json_traverse(json_out, self._image_dim_json_path[1])) - print("image dim:",iwidth,iheight) + print("image dim:", iwidth, iheight) # we're done with the requests object, lets delete it so we can do more! json_out = None @@ -858,7 +858,7 @@ def fetch(self, refresh_url=None): print("original URL:", image_url) if iwidth < iheight: image_url = self.image_converter_url(image_url, - int( self._image_resize[1] * self._image_resize[1] + int(self._image_resize[1] * self._image_resize[1] / self._image_resize[0]), self._image_resize[1]) else: @@ -882,7 +882,7 @@ def fetch(self, refresh_url=None): print(error) raise RuntimeError("wget didn't write a complete file") if iwidth < iheight: - pwidth = int( self._image_resize[1] * self._image_resize[1] / self._image_resize[0]) + pwidth = int(self._image_resize[1] * self._image_resize[1] / self._image_resize[0]) self.set_background(filename, (self._image_position[0] + int((self._image_resize[0] - pwidth) / 2), self._image_position[1])) else: From 01b46909eaf80d333d302434308e1d2d21c11477 Mon Sep 17 00:00:00 2001 From: Dan Cogliano Date: Mon, 6 Jan 2020 14:44:04 -0500 Subject: [PATCH 3/5] long line fixes --- adafruit_pyportal.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index 6c66fbe..f2a6478 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -858,7 +858,8 @@ def fetch(self, refresh_url=None): print("original URL:", image_url) if iwidth < iheight: image_url = self.image_converter_url(image_url, - int(self._image_resize[1] * self._image_resize[1] + int(self._image_resize[1] + * self._image_resize[1] / self._image_resize[0]), self._image_resize[1]) else: @@ -882,8 +883,12 @@ def fetch(self, refresh_url=None): print(error) raise RuntimeError("wget didn't write a complete file") if iwidth < iheight: - pwidth = int(self._image_resize[1] * self._image_resize[1] / self._image_resize[0]) - self.set_background(filename, (self._image_position[0] + int((self._image_resize[0] - pwidth) / 2), + pwidth = int(self._image_resize[1] * + self._image_resize[1] / self._image_resize[0]) + self.set_background(filename, + (self._image_position[0] + + int((self._image_resize[0] + - pwidth) / 2), self._image_position[1])) else: self.set_background(filename, self._image_position) From f9c5048ea3dca193d2ca78638aab054d1e2c1ac1 Mon Sep 17 00:00:00 2001 From: Dan Cogliano Date: Mon, 6 Jan 2020 14:49:10 -0500 Subject: [PATCH 4/5] Misc. fixes --- adafruit_pyportal.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index f2a6478..9388172 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -858,7 +858,7 @@ def fetch(self, refresh_url=None): print("original URL:", image_url) if iwidth < iheight: image_url = self.image_converter_url(image_url, - int(self._image_resize[1] + int(self._image_resize[1] * self._image_resize[1] / self._image_resize[0]), self._image_resize[1]) @@ -883,13 +883,13 @@ def fetch(self, refresh_url=None): print(error) raise RuntimeError("wget didn't write a complete file") if iwidth < iheight: - pwidth = int(self._image_resize[1] * + pwidth = int(self._image_resize[1] * self._image_resize[1] / self._image_resize[0]) self.set_background(filename, - (self._image_position[0] - + int((self._image_resize[0] + (self._image_position[0] + + int((self._image_resize[0] - pwidth) / 2), - self._image_position[1])) + self._image_position[1])) else: self.set_background(filename, self._image_position) From b4d38f34cff60529432fbef319c31a786c87f25c Mon Sep 17 00:00:00 2001 From: Dan Cogliano Date: Mon, 6 Jan 2020 14:53:36 -0500 Subject: [PATCH 5/5] disabled too-many-lines pylint message --- adafruit_pyportal.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index 9388172..66a8044 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -88,6 +88,7 @@ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyPortal.git" # pylint: disable=line-too-long +# pylint: disable=too-many-lines # you'll need to pass in an io username, width, height, format (bit depth), io key, and then url! 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" # you'll need to pass in an io username and key