Numpy和16位PGM [英] Numpy and 16-bit PGM
问题描述
使用numpy在Python中读取16位PGM图像的有效而清晰的方法是什么?
What is an efficient and clear way to read 16-bit PGM images in Python with numpy?
由于PIL错误,我无法使用PIL加载16位PGM图像 .我可以使用以下代码读取标题:
I cannot use PIL to load 16-bit PGM images due to a PIL bug. I can read in the header with the following code:
dt = np.dtype([('type', 'a2'),
('space_0', 'a1', ),
('x', 'a3', ),
('space_1', 'a1', ),
('y', 'a3', ),
('space_2', 'a1', ),
('maxval', 'a5')])
header = np.fromfile( 'img.pgm', dtype=dt )
print header
这将打印正确的数据:('P5', ' ', '640', ' ', '480', ' ', '65535')
但是我感觉这并不是最好的方法.除此之外,我不知道如何找出如何用偏移量为size(header)
的16位以y(在本例中为640x480)读取x的以下数据.
This prints the correct data: ('P5', ' ', '640', ' ', '480', ' ', '65535')
But I have a feeling that is not quite the best way. And beyond that, I'm having trouble how to figure out how to read in the following data of x by y (in this case 640x480) by 16-bit with the offset of size(header)
.
添加了图像
读取和显示图像的MATLAB代码为:
MATLAB code to read and display the image is:
I = imread('foo.pgm');
imagesc(I);
看起来像这样:
推荐答案
import re
import numpy
def read_pgm(filename, byteorder='>'):
"""Return image data from a raw PGM file as numpy array.
Format specification: http://netpbm.sourceforge.net/doc/pgm.html
"""
with open(filename, 'rb') as f:
buffer = f.read()
try:
header, width, height, maxval = re.search(
b"(^P5\s(?:\s*#.*[\r\n])*"
b"(\d+)\s(?:\s*#.*[\r\n])*"
b"(\d+)\s(?:\s*#.*[\r\n])*"
b"(\d+)\s(?:\s*#.*[\r\n]\s)*)", buffer).groups()
except AttributeError:
raise ValueError("Not a raw PGM file: '%s'" % filename)
return numpy.frombuffer(buffer,
dtype='u1' if int(maxval) < 256 else byteorder+'u2',
count=int(width)*int(height),
offset=len(header)
).reshape((int(height), int(width)))
if __name__ == "__main__":
from matplotlib import pyplot
image = read_pgm("foo.pgm", byteorder='<')
pyplot.imshow(image, pyplot.cm.gray)
pyplot.show()
这篇关于Numpy和16位PGM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!