def image_from_mask(mask):
    """
    Convert a binary mask into a PIL image.
    """
    import PIL
    mask_image = PIL.Image.new('1', mask.shape)
    pixels = mask_image.load()
    for i in range(mask_image.size[0]):
        for j in range(mask_image.size[1]):
            pixels[i, j] = int(mask[i, j])
    return mask_image
