2828* Author(s): Matt Land, Brooke Storm, Sam McGahan
2929
3030"""
31-
31+ #import logging
3232
3333def load (file , magic_number , header , * , bitmap = None , palette = None ):
3434 # TODO: remove unused variables later
@@ -50,14 +50,15 @@ def load(file, magic_number, header, *, bitmap=None, palette=None):
5050 for x in range (width ):
5151 # Takes int and converts to an 8 bit
5252 while True :
53- bit = file .read (1 ) # type: byte
54- if not bit .isdigit ():
53+ byte = file .read (1 ) # type: byte
54+ if not byte .isdigit ():
5555 break
56- pixel += bit
56+ pixel += byte
5757
5858 int_pixel = int ("" .join (["%c" % char for char in pixel ]))
5959 bitmap [x , y ] = int_pixel
6060 colors .add (int_pixel )
61+ # logging.info(f'{x}, {y}, {byte}, {int_pixel}')
6162 if palette :
6263 palette = palette (len (colors ))
6364 for counter , color in enumerate (colors ):
@@ -71,10 +72,13 @@ def load(file, magic_number, header, *, bitmap=None, palette=None):
7172 if magic_number == b'P5' : # To handle binary PGM files.
7273 for y in range (height ):
7374 for x in range (width ):
74- bit = file .read (2 )
75- if not bit . isdigit () :
75+ byte = file .read (1 )
76+ if byte == b"" :
7677 raise ValueError ("ran out of file unexpectedly" )
77- bitmap [x , y ] = bit
78+ int_pixel = int .from_bytes (byte , "little" )
79+ bitmap [x , y ] = int_pixel
80+ colors .add (int_pixel )
81+
7882
7983 if palette :
8084 palette = palette (len (colors ))
@@ -86,7 +90,4 @@ def load(file, magic_number, header, *, bitmap=None, palette=None):
8690
8791 return bitmap , palette
8892
89-
90-
91-
9293 raise NotImplementedError ("Was not able to send image" )
0 commit comments