88
88
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyPortal.git"
89
89
90
90
# pylint: disable=line-too-long
91
+ # pylint: disable=too-many-lines
91
92
# you'll need to pass in an io username, width, height, format (bit depth), io key, and then url!
92
93
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"
93
94
# you'll need to pass in an io username and key
@@ -143,6 +144,8 @@ class PyPortal:
143
144
of the width and height you want. Defaults to ``None``.
144
145
:param image_position: The position of the image on the display as an (x, y) tuple. Defaults to
145
146
``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``.
146
149
:param success_callback: A function we'll call if you like, when we fetch data successfully.
147
150
Defaults to ``None``.
148
151
: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,
165
168
text_font = None , text_position = None , text_color = 0x808080 ,
166
169
text_wrap = False , text_maxlen = 0 , text_transform = None ,
167
170
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 ,
169
172
caption_text = None , caption_font = None , caption_position = None ,
170
173
caption_color = 0x808080 , image_url_path = None ,
171
174
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,
368
371
self ._image_url_path = image_url_path
369
372
self ._image_resize = image_resize
370
373
self ._image_position = image_position
374
+ self ._image_dim_json_path = image_dim_json_path
371
375
if image_json_path or image_url_path :
372
376
if self ._debug :
373
377
print ("Init image path" )
@@ -838,6 +842,13 @@ def fetch(self, refresh_url=None):
838
842
print ("Error finding image data. '" + error .args [0 ] + "' not found." )
839
843
self .set_background (self ._default_bg )
840
844
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
+
841
852
# we're done with the requests object, lets delete it so we can do more!
842
853
json_out = None
843
854
r = None
@@ -846,9 +857,16 @@ def fetch(self, refresh_url=None):
846
857
if image_url :
847
858
try :
848
859
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 ])
852
870
print ("convert URL:" , image_url )
853
871
# convert image to bitmap and cache
854
872
#print("**not actually wgetting**")
@@ -865,7 +883,17 @@ def fetch(self, refresh_url=None):
865
883
except RuntimeError as error :
866
884
print (error )
867
885
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
+
869
897
except ValueError as error :
870
898
print ("Error displaying cached image. " + error .args [0 ])
871
899
self .set_background (self ._default_bg )
0 commit comments