def binary_partition_image(image, min_w=1, min_h=1, depth=0, max_depth=-1):
    """Return a bsp of [pos, axis, [before_node, after_node]] nodes where leaf
    nodes == None.

    If max_depth < 0 this function will continue until all leaf nodes have
    been found, if it is >= 0 leaf nodes will be created at that depth.

    min_w and min_h are the minimum width or height of a partition.

    """
    if max_depth >= 0 and depth >= max_depth:
        return None
    partition = get_best_partition(image, min_w=min_w, min_h=min_h)
    if partition is None:
        return None
    pos, axis = partition
    if axis == 0:
        p1 = binary_partition_image(
            image[pos:], min_w, min_h, depth + 1, max_depth)
        p2 = binary_partition_image(
            image[:pos], min_w, min_h, depth + 1, max_depth)
    elif axis == 1:
        p1 = binary_partition_image(
            image[:, pos:], min_w, min_h, depth + 1, max_depth)
        p2 = binary_partition_image(
            image[:, :pos], min_w, min_h, depth + 1, max_depth)
    return [pos, axis, [p1, p2]]
Return a bsp of [pos, axis, [before_node, after_node]] nodes where leaf
    nodes == None.

    If max_depth < 0 this function will continue until all leaf nodes have
    been found, if it is >= 0 leaf nodes will be created at that depth.

    min_w and min_h are the minimum width or height of a partition.