## reads a B/W bitmap and copies the content into a vector where each byte represents 8 pixels
  
import os, math
from scipy import misc
from imageio import imread

def image_to_vector(image, startx, starty, stopx, stopy):
    byte_vector = []
    byte = 0
    bit_pos = 0
    def add_bit(value, byte, bit_pos):
        if value == 1:
            byte = byte + pow(2, bit_pos)
        bit_pos = bit_pos + 1
        if bit_pos > 7:
            byte_vector.append(byte)
            byte = 0
            bit_pos = 0
        return [byte, bit_pos]
    ## chars are low to high (4 pixels/byte: 01, 23, 45, 67)!
    for image_row in range(starty, stopy):
        for image_column in range(startx, stopx):
            #print image[image_row][image_column]
            if image[image_row][image_column] == 0:
                [byte, bit_pos] = add_bit(0,byte, bit_pos)
            if image[image_row][image_column] == 255:
                [byte, bit_pos] = add_bit(1,byte, bit_pos)
    if bit_pos > 0:
        byte_vector.append(byte)
    return byte_vector
