Python Numpy和16位PGM
使用numpy在Python中读取16位PGM图像的有效且清晰的方法是什么 我无法使用PIL加载16位PGM图像。我可以用以下代码读取标题:Python Numpy和16位PGM,python,numpy,16-bit,pgm,Python,Numpy,16 Bit,Pgm,使用numpy在Python中读取16位PGM图像的有效且清晰的方法是什么 我无法使用PIL加载16位PGM图像。我可以用以下代码读取标题: dt = np.dtype([('type', 'a2'), ('space_0', 'a1', ), ('x', 'a3', ), ('space_1', 'a1', ), ('y', 'a3', ), (
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位x×y(在本例中为640x480)的数据
编辑:添加图像
读取和显示图像的MATLAB代码为:
I = imread('foo.pgm');
imagesc(I);
看起来是这样的:
据我所知,标题信息可以用空格、回车符或其他字符分隔。如果您的文件之间有空格(如果没有空格,请通知我),您可以执行以下操作:
with open('img.pgm') as f:
lines = f.readlines()
data = np.array([line.split() for line in lines[1:]], dtype=np.int16).T
class Header(object):
def __init__(self, type, width, height, maxval):
self.type = type
self.width = int(width)
self.height = int(height)
self.maxval = int(maxval)
h = Header(*lines[0].split()[:4])
您的数据现在是int16格式的数组
假设您仍然对标题信息感兴趣,可以执行以下操作:
with open('img.pgm') as f:
lines = f.readlines()
data = np.array([line.split() for line in lines[1:]], dtype=np.int16).T
class Header(object):
def __init__(self, type, width, height, maxval):
self.type = type
self.width = int(width)
self.height = int(height)
self.maxval = int(maxval)
h = Header(*lines[0].split()[:4])
以便您可以对照读取行检查图像数据:
assert (h.width, h.height) == data.shape
assert h.maxval >= data.max()
编辑:图像数据为二进制,文件必须以“rb”的形式打开,并在标题信息之后读取:
import numpy as np
def as_array(filepath):
f = open(filepath, 'r')
w, h = size = tuple(int(v) for v in next(f).split()[1:3])
data_size = w * h * 2
f.seek(0, 2)
filesize = f.tell()
f.close()
i_header_end = filesize - (data_size)
f = open(filepath, 'rb')
f.seek(i_header_end)
buffer = f.read()
f.close()
# convert binary data to an array of the right shape
data = np.frombuffer(buffer, dtype=np.uint16).reshape((w, h))
return data
a = as_array('foo.pgm')
我不太熟悉PGM格式,但一般来说,您只需要使用
numpy.fromfile
fromfile
将从您传递到它的文件指针所在的任何位置开始,因此您可以简单地查找(或读取)标题的末尾,然后使用fromfile
读取中的其余部分
您需要使用infle.readline()
而不是next(infle)
另一方面,您在注释中指向的“foo.pgm”文件似乎在标题中指定了错误的行数
如果您要读取大量可能存在此问题的文件,可以用零填充数组或截断它,如下所示
import numpy as np
with open('foo.pgm', 'r') as infile:
header = next(infile)
width, height, maxval = [int(item) for item in header.split()[1:]]
image = np.fromfile(infile, dtype=np.uint16)
if image.size < width * height:
pad = np.zeros(width * height - image.size, dtype=np.uint16)
image = np.hstack([image, pad])
if image.size > width * height:
image = image[:width * height]
image = image.reshape((height, width))
将numpy导入为np
以open('foo.pgm','r')作为填充:
标题=下一个(填充)
宽度、高度、maxval=[int(item)表示标头中的项。拆分()[1:]
image=np.fromfile(infle,dtype=np.uint16)
如果image.size<宽度*高度:
pad=np.zero(宽度*高度-image.size,数据类型=np.uint16)
image=np.hstack([image,pad])
如果image.size>宽度*高度:
图像=图像[:宽度*高度]
图像=图像。重塑((高度、宽度))
实际上,头后面的“字符串”是文件中的二进制文件。我在下面解决了这个问题(发现如下:
ndarray:[2047 2047 2047…,540 539 539]
),但还有一个问题:文件不够长;仅统计289872个数字,而不是640*480
我为我的夸大其词感到非常抱歉,为此我上了一堂课
import numpy as np
import Image
class PGM(object):
def __init__(self, filepath):
with open(filepath) as f:
# suppose all header info in first line:
info = f.readline().split()
self.type = info[0]
self.width, self.height, self.maxval = [int(v) for v in info[1:]]
size = self.width * self.height
lines = f.readlines()
dt = [np.int8, np.int16][self.maxval > 255]
try:
# this will work if lines are integers separated by e.g. spaces
self.data = np.array([l.split() for l in lines], dtype=dt).T
except ValueError:
# data is binary
data = np.fromstring(lines[0], dtype=dt)
if data.size < size:
# this is the case for the 'db.tt/phaR587 (foo.pgm)'
#raise ValueError('data binary string probably uncomplete')
data = np.hstack((data, np.zeros(size-data.size)))
self.data = data[:size].reshape((self.width, self.height))
assert (self.width, self.height) == self.data.shape
assert self.maxval >= self.data.max()
self._img = None
def get_img(self):
if self._img is None:
# only executed once
size = (self.width, self.height)
mode = 'L'
data = self.data
self.img = Image.frombuffer(mode, size, data)
return self.img
Image = property(get_img)
mypgm = PGM('foo.pgm')
mypgm.Image
将numpy导入为np
导入图像
类别PGM(对象):
定义初始化(self,filepath):
将open(filepath)作为f:
#假设第一行中的所有标题信息:
info=f.readline().split()
self.type=info[0]
self.width、self.height、self.maxval=[int(v)表示信息中的v[1:]
尺寸=自宽*自高
行=f.读行()
dt=[np.int8,np.int16][self.maxval>255]
尝试:
#如果行是由空格等分隔的整数,则此操作有效
self.data=np.array([l.split()表示行中的l],dtype=dt).T
除值错误外:
#数据是二进制的
data=np.fromstring(第[0]行,dtype=dt)
如果data.size=self.data.max()
self.\u img=无
def get_img(自我):
如果self.\u img为无:
#只执行一次
大小=(自宽、自高)
模式='L'
data=self.data
self.img=Image.frombuffer(模式、大小、数据)
返回self.img
图像=属性(获取图像)
mypgm=PGM('foo.PGM')
mypgm.图像
编辑:乔·金顿提出的用零填充图像的好主意 感谢@joe kington的回答帮助我们解决了这个问题。解决办法如下 有一点额外的工作,不硬编码已知的头长度(17个字节的长度) 本例中),但要从页眉确定它。PGM标准规定,标题通常以换行符结尾,但可以以任何空格结尾。我认为这段代码将在PGM上中断,PGM使用非换行空格作为标题的结尾。在这种情况下,标题大小由包含width、height和maxsize的变量的大小决定,加上两个字节的“P5”,再加上4个字节的空白 其他情况下,如果宽度或高度大于int(非常大的图像),则可能会中断。或者如果PGM是8位而不是16位(可以通过maxval以及可能的宽度、高度和文件大小来确定)
重新导入
进口numpy
def read_pgm(文件名,字节顺序='>'):
“”“将原始PGM文件中的图像数据作为numpy数组返回。
格式规范:http://netpbm.sourceforge.net/doc/pgm.html
"""
将open(filename,'rb')作为f:
缓冲区=f.read()
尝试:
标题、宽度、高度,最大值=重新搜索(
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)*”),缓冲区.groups()
除属性错误外:
raise VALUERROR(“不是原始PGM文件:“%s”%”%filename)
返回numpy.frombuffer(buffer,
如果int(maxval)<256 else字节顺序+'u2',则dtype='u1',
计数=整数(宽度)*整数(高度),
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()