def save_points( out, path1, points1, path2=None, points2=None):
    """
    Save points to a text file.
    :param out: Path of the output file to write
    :param path1: Path of the first image, or None to omit.
    :param points1: Points picked in first image.
    :param path2: Path of second image, or None to omit.
    :param points2: Points picked from the second image, or None.
    :returns : the number of points or point-pairs written to the file.
    """
    n = 0
    if out is not None:
        with open(out, 'w') as f:
            if points1 is not None and points2 is None:  # single-image mode
                if path1 is not None:
                    f.write(path1 + '\n')
                for px, py in points1:
                    f.write('%f,%f\n' % (px, py))
                    n += 1
            elif points1 is not None and points2 is not None:  # multi-image mode
                if path1 is not None and path2 is not None:
                    f.write(path1 + '\n')
                    f.write(path2 + '\n')
                for _p1, _p2 in zip(points1, points2):
                    f.write('%f,%f,    %f,%f\n' % (_p1[0], _p1[1], _p2[0], _p2[1]))
                    n += 1
    return n