I am interested in reading a pgm file in python as a numerical file/matrix
Right now I open the file with
f = open('/home/matthew/NCM/mdb001.pgm', 'rb')
When I read the first line, it looks as expected
r.readline()
produces
'P5\n'
and the next line is fine
'1024 1024\n'
and the next
'255\n'
but then I get a series of gibberish. It looks like some hex values mixed in with other stuff.
I don't want to view the file as an image picture, I just want to see it in this format.
After reading the header as you've shown, you've got the width (1024) the height (the next 1024) and the depth (255). To get the pixel data it is easiest to read them byte-by-byte:
def read_pgm(pgmf):
"""Return a raster of integers from a PGM as a list of lists."""
assert pgmf.readline() == 'P5\n'
(width, height) = [int(i) for i in pgmf.readline().split()]
depth = int(pgmf.readline())
assert depth <= 255
raster = []
for y in range(height):
row = []
for y in range(width):
row.append(ord(pgmf.read(1)))
raster.append(row)
return raster
This code will only work for 8-bit depth images which is why the assert
statement is present.
It is legal for a PGM file to have the header information on one line as in:
P5 1024 1024 15
If you do encounter such a file, read_pgm
will fail noisily; the code to handle such cases is left as an exercise for the reader.