如何在ASCII模式下读取pbm文件的大小?

时间:2016-12-01 04:47:44

标签: 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 image
    f.close()   

当我在我的ubuntu14.04操作系统中编译它时,它显示错误。任何建议将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

  

宽度和高度就在文件中,在第一行之后的第一行中,跳过注释。这不是.size的用途;你需要阅读并解析文件。

class AnimalType {
    private static final Map<String, Animal> animals  = new HashMap<String, Animal>();

    static {
        // Populating map with default animals
        addAnimal("big","BELLOWWWWW"); // bison
        addAnimal("small","SQUEEEEEAK"); // mouse
        addAnimal("lazy","ROARRRRR"); // lion
        addAnimal("loyal","WOOF "); // dog
    }

    public static void addAnimal(String criteria, final String sound) {
        // Assigning a anonymous implementation of animal to the given criteria
        animals.put(criteria, new Animal() {
            @Override
            public void talk() {
                System.out.println(sound);
            }
        });
    }

    public static Animal getAnimal(String criteria) {
        // Returning an animal from the animals map
        return animals.get(criteria);
    }
}