import itertools


def get_idx_by_range(img_shapes, row_range, col_range):
    """
    get idx of img slices from selected area
    args:
      row_range: (list) range for rows
      col_range: (list) range for cols
      img_shapes: (list) nb_rows, nb_cols for img_slices
    return selected idx, shapes
    """
    assert row_range[0] < row_range[1], "row_range, max should larger than min"
    assert col_range[0] < col_range[1], "col_range, max should larger than min"
    assert row_range[1] <= img_shapes[0], "max range of row should less that nb_rows"
    assert col_range[1] <= img_shapes[1], "max range of col should less that nb_cols"
    selected_idx = []
    row_rg = range(row_range[0], row_range[1], 1)
    col_rg = range(col_range[0], col_range[1], 1)
    for row, col in itertools.product(row_rg, col_rg):
        selected_idx.append(row * img_shapes[1] + col)

    selected_shapes = [row_range[1] -
                       row_range[0], col_range[1] - col_range[0]]
    return selected_idx, selected_shapes