def align8to32(bytes, width, mode):
 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)] + ('\x00' * extra_padding)))
 return ''.join(new_data)

def align8to32 ( bytes , width , mode ) : 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 ) ] + ( '\x00' * extra_padding ) ) ) return '' . join ( new_data )
converts each scanline of data from 8 bit to 32 bit aligned .