Skip to content

Commit 8cb69c4

Browse files
committed
Place saving update
- Bitmap is seemingly working - Palette is not working - Ascii might have broke? going to do more testing later.
1 parent 1c1a164 commit 8cb69c4

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

adafruit_imageload/pnm/pgm/__init__.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* Author(s): Matt Land, Brooke Storm, Sam McGahan
2929
3030
"""
31-
31+
#import logging
3232

3333
def 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")

adafruit_imageload/tests/test_pnm_load.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ def test_load_works_p5_binary(self):
180180
self.assertEqual(8, bitmap.width)
181181
self.assertEqual(8, bitmap.height)
182182
bitmap.validate()
183-
self.assertEqual(6, palette.colors)
183+
self.assertEqual(8, palette.num_colors)
184+
185+
184186

185187

186188
class TestPnmLoad(TestCase):

0 commit comments

Comments
 (0)