def align8to32(bytes, width, mode):
    """
    converts each scanline of data from 8 bit to 32 bit aligned
    """
    bits_per_pixel = {'1': 1, 'L': 8, 'P': 8}[mode]
    bits_per_line = bits_per_pixel * width
    full_bytes_per_line, remaining_bits_per_line = divmod(bits_per_line, 8)
    bytes_per_line = full_bytes_per_line + (1 if remaining_bits_per_line else 0
        )
    extra_padding = -bytes_per_line % 4
    if not extra_padding:
        return bytes
    new_data = []
    for i in range(len(bytes) // bytes_per_line):
        new_data.append(bytes[i * bytes_per_line:(i + 1) * bytes_per_line] +
            b'\x00' * extra_padding)
    return b''.join(new_data)
