@@ -617,21 +617,27 @@ def mp_icon(filename):
617617 '''load an icon from the data directory'''
618618 # we have to jump through a lot of hoops to get an OpenCV image
619619 # when we may be in a package zip file
620+ raw = None
620621 try :
621622 import pkg_resources
622623 name = __name__
623624 if name == "__main__" :
624625 name = "MAVProxy.modules.mavproxy_map.mp_tile"
625- stream = pkg_resources .resource_stream (name , "data/%s" % filename ). read ( )
626- raw = np .fromstring (stream , dtype = np .uint8 )
626+ stream = pkg_resources .resource_stream (name , f "data/{ filename } " )
627+ raw = np .frombuffer (stream . read () , dtype = np .uint8 )
627628 except Exception :
628629 try :
629- stream = open (os .path .join (os .path .dirname (__file__ ), 'data' , filename )). read ()
630- raw = np .fromstring ( stream , dtype = np .uint8 )
630+ with open (os .path .join (os .path .dirname (__file__ ), 'data' , filename ), 'rb' ) as f :
631+ raw = np .frombuffer ( f . read () , dtype = np .uint8 )
631632 except Exception :
632- #we're in a Windows exe, where pkg_resources doesn't work
633- import pkgutil
634- raw = pkgutil .get_data ( 'MAVProxy' , 'modules//mavproxy_map//data//' + filename )
633+ try :
634+ import pkgutil
635+ stream = pkgutil .get_data ('MAVProxy' , f'modules/mavproxy_map/data/{ filename } ' )
636+ raw = np .frombuffer (stream , dtype = np .uint8 )
637+ except Exception as e :
638+ print (f"Failed to load image '{ filename } ': { e } " )
639+ return None
640+
635641 img = cv2 .imdecode (raw , cv2 .IMREAD_COLOR )
636642 return img
637643
0 commit comments