def pbmb_scanlines(infile, width, height):
    """
    Generates boxed rows in flat pixel format, from the PBM input file.

    It assumes that the input file is in a "Netpbm-like"
    binary format, and is positioned at the beginning of the first
    pixel.  The number of pixels to read is taken from the image
    dimensions (`width`, `height`).
    """
    def int2bitseq(byte):
        """Crude but simple way to unpack byte to bits"""
        if isinstance(byte, (str, bytes)):
            byte = ord(byte)
        res = bytearray()
        for _ in range(8):
            (byte, bit) = divmod(byte, 2)
            res.append(bit)
        return res

    tail = bytearray()
    for _ in range(height):
        while len(tail) < width:
            byte = infile.read(1)
            tail.extend(int2bitseq(byte))
        yield bytearray(tail[:width])
        tail = tail[width:]