Skip to content

Commit 531951c

Browse files
committed
starting binary pgm work
1 parent d984ca0 commit 531951c

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

adafruit_imageload/pnm/pgm/__init__.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,20 @@ def load(file, magic_number, header, *, bitmap=None, palette=None):
3535
width = header[0]
3636
height = header[1]
3737
max_colors = header[2]
38-
min_color = 1 # probably don't need this
39-
columns = height
4038
bitmap = bitmap(width, height, max_colors)
39+
colors = set()
40+
pixel = bytearray()
4141

4242

4343
if max_colors > 256:
4444
# raise exception
4545
raise NotImplementedError("16 bit grayscale not supported")
4646

47-
if magic_number == b'P2':
47+
if magic_number == b'P2': # To handle ascii PGM files.
4848
# Handle ascii
49-
colors = set()
5049
for y in range(height):
5150
for x in range(width):
5251
# Takes int and converts to an 8 bit
53-
pixel = bytearray()
54-
5552
while True:
5653
bit = file.read(1) # type: byte
5754
if not bit.isdigit():
@@ -71,7 +68,25 @@ def load(file, magic_number, header, *, bitmap=None, palette=None):
7168
return bitmap, palette
7269

7370

74-
if magic_number == b'P5':
75-
raise NotImplementedError("This is a Binary file")
71+
if magic_number == b'P5': # To handle binary PGM files.
72+
for y in range(height):
73+
for x in range(width):
74+
bit = file.read(2)
75+
if not bit.isdigit():
76+
raise ValueError("ran out of file unexpectedly")
77+
bitmap[x, y] = bit
78+
79+
if palette:
80+
palette = palette(len(colors))
81+
for counter, color in enumerate(colors):
82+
color_bytearray = bytearray()
83+
for i in range(3):
84+
color_bytearray += bytes([color])
85+
palette[counter] = color_bytearray
86+
87+
return bitmap, palette
88+
89+
90+
7691

7792
raise NotImplementedError("Was not able to send image")

0 commit comments

Comments
 (0)