#NEWLINE# def _read_data( self , fh , byteorder = '>' ) : #NEWLINE# #INDENT# 'Return image data from open file as numpy array.' #NEWLINE# fh . seek ( len ( self . header ) ) #NEWLINE# data = fh . read ( ) #NEWLINE# dtype = ( 'u1' if ( self . maxval < 256 ) else ( byteorder + 'u2' ) ) #NEWLINE# depth = ( 1 if ( self . magicnum == b'P7 332' ) else self . depth ) #NEWLINE# shape = [ ( - 1 ) , self . height , self . width , shape ] #NEWLINE# size = numpy . prod ( shape [ 1 : ] ) #NEWLINE# if ( self . magicnum in b'P1P2P3' ) : #NEWLINE# #INDENT# data = numpy . array ( data . split ( None , size ) [ : size ] , dtype ) #NEWLINE# data = data . reshape ( shape ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . maxval == 1 ) : #NEWLINE# #INDENT# shape [ 2 ] = int ( math . ceil ( ( self . width / 8 ) ) ) #NEWLINE# data = numpy . frombuffer ( data , dtype ) . reshape ( shape ) #NEWLINE# data = numpy . unpackbits ( data , axis = ( - 2 ) ) [ : , : , : self . width , : ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# data = numpy . frombuffer ( data , dtype ) #NEWLINE# data = data [ : ( size * ( data . size // size ) ) ] . reshape ( shape ) #NEWLINE# #UNINDENT# #UNINDENT# if ( data . shape [ 0 ] < 2 ) : #NEWLINE# #INDENT# data = data . reshape ( data . shape [ 1 : ] ) #NEWLINE# #UNINDENT# if ( data . shape [ ( - 1 ) ] < 2 ) : #NEWLINE# #INDENT# data = data . reshape ( data . shape [ : ( - 1 ) ] ) #NEWLINE# #UNINDENT# if ( self . magicnum == b'P7 332' ) : #NEWLINE# #INDENT# rgb332 = numpy . array ( list ( numpy . ndindex ( 8 , 8 , 4 ) ) , numpy . uint8 ) #NEWLINE# rgb332 *= [ 36 , 36 , 85 ] #NEWLINE# data = numpy . take ( rgb332 , data , axis = 0 ) #NEWLINE# #UNINDENT# return data