#NEWLINE# def _load_cv2( img , grayscale = None ) : #NEWLINE# #INDENT# if ( grayscale is None ) : #NEWLINE# #INDENT# grayscale = GRAYSCALE_DEFAULT #NEWLINE# #UNINDENT# if isinstance ( img , str ) : #NEWLINE# #INDENT# if grayscale : #NEWLINE# #INDENT# img_cv = cv2 . imread ( img , LOAD_GRAYSCALE ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# img_cv = cv2 . imread ( img , LOAD_COLOR ) #NEWLINE# #UNINDENT# if ( img_cv is None ) : #NEWLINE# #INDENT# raise IOError ( ( 'Failed to read %s because file is missing, has improper permissions, or is an unsupported or invalid format' % img ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( img , numpy . ndarray ) : #NEWLINE# #INDENT# if ( grayscale and ( len ( img . shape ) == 3 ) ) : #NEWLINE# #INDENT# img_cv = cv2 . cvtColor ( img , cv2 . COLOR_BGR2GRAY ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if hasattr ( img , 'convert' ) : #NEWLINE# #INDENT# img_array = numpy . array ( img . convert ( 'RGB' ) ) #NEWLINE# img_cv = img_array [ : , : , : : ( - 1 ) ] . copy ( ) #NEWLINE# if grayscale : #NEWLINE# #INDENT# img_cv = cv2 . cvtColor ( img_cv , cv2 . COLOR_BGR2GRAY ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise TypeError ( 'expected an image filename, OpenCV numpy array, or PIL image' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return img_cv