'cb = the codebook
patchsize = the patch size (ususally 11x11)
count = total codes
patch_arrangement = how are the patches grided in the image (eg 128 = (8x16) 256=(16x16) )
spacersz = the number of pixels between patches'

def _codebook2Img(self, cb, patchsize, count, patch_arrangement, spacersz):

    w = ((patchsize[0] * patch_arrangement[0]) + ((patch_arrangement[0] + 1) * spacersz))
    h = ((patchsize[1] * patch_arrangement[1]) + ((patch_arrangement[1] + 1) * spacersz))
    bm = cv.CreateImage((w, h), cv.IPL_DEPTH_8U, 1)
    cv.Zero(bm)
    img = Image(bm)
    count = 0
    for widx in range(patch_arrangement[0]):
        for hidx in range(patch_arrangement[1]):
            x = ((widx * patchsize[0]) + ((widx + 1) * spacersz))
            y = ((hidx * patchsize[1]) + ((hidx + 1) * spacersz))
            temp = Image(cb[count, :].reshape(patchsize[0], patchsize[1]))
            img.blit(temp, pos=(x, y))
            count = (count + 1)
    return img
