import struct class PnmImage(object): def __init__(self): self._filename = None self._image = [] self._version = None self._maxval = None self._width = 0 self._height = 0 def reset(self): """Reset all image data (dims, data,...)""" self._filename = None self._image = [] self._version = None self._maxval = None self._width = 0 self._height = 0 def _load_bin_pbm(self, fd): """Load image data from a file of PBM binary format :param fd: file descriptor of the file starting to the image data so the version and dimensions should have been skipped """ c = fd.read(1) i = 0 size = self._width * self._height while len(c) > 0: c = struct.unpack("= size: break if i >= size: break c = fd.read(1) def _load_bin_pgm(self, fd): """Load image data from a file of PGM binary format :param fd: file descriptor of the file starting to the image data so the version and dimensions should have been skipped """ maxval = fd.readline().decode("utf-8").strip() self._maxval = int(maxval) c = fd.read(1) while len(c) > 0: c = struct.unpack("