Image 如何在ASCII模式下读取pbm文件的大小? 让我们考虑这个例子:

Image 如何在ASCII模式下读取pbm文件的大小? 让我们考虑这个例子:,image,python-2.7,file,Image,Python 2.7,File,pbm文件“imFile.pbm”包含如下像素: P1 # Comment 9 6 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 如何确定图像的宽度和高度。我使用了以下代码,但失败了 with open("imFile.pbm", 'rb') as f: image = f.size print

pbm文件“imFile.pbm”包含如下像素:

P1
# Comment
9 6
0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0      
如何确定图像的宽度和高度。我使用了以下代码,但失败了

 with open("imFile.pbm", 'rb') as f:
    image = f.size
    print image
    f.close()   
当我在ubuntu14.04操作系统中编译它时,它显示错误。如有任何建议,将不胜感激。先谢谢你


宽度和高度就在文件中,在第一行之后的第一行,跳过注释。不是这样的。大小是用来;您需要读取并解析该文件


宽度和高度就在文件中,在第一行之后的第一行,跳过注释。不是这样的。大小是用来;您需要读取并解析该文件


宽度和高度就在文件中,在第一行之后的第一行,跳过注释。这不是
.size
的用途;您需要读取并解析文件。@Amadan--我该怎么做?你能解释一下吗?宽度和高度就在文件中,在第一行之后的第一行,跳过注释。这不是
.size
的用途;您需要读取并解析文件。@Amadan--我该怎么做?你能给我解释一下吗?
with open("imFile.pbm", 'r') as f:
    lines = f.readlines()
lines.pop(0)                       # skip header
line = lines.pop(0)                # get next line
while line.startswith("#"):        # repeat till that line is not a comment
    line = lines.pop(0)
width, height= line.split()        # split the first non-comment lin
print("%s x %s" % (width, height)) # => 9 x 6