You could do something like this -
import numpy as np
from scipy.spatial import distance
def sortpts_clockwise(A):
# Sort A based on Y(col-2) coordinates
sortedAc2 = A[np.argsort(A[:,1]),:]
# Get top two and bottom two points
top2 = sortedAc2[0:2,:]
bottom2 = sortedAc2[2:,:]
# Sort top2 points to have the first row as the top-left one
sortedtop2c1 = top2[np.argsort(top2[:,0]),:]
top_left = sortedtop2c1[0,:]
# Use top left point as pivot & calculate sq-euclidean dist against
# bottom2 points & thus get bottom-right, bottom-left sequentially
sqdists = distance.cdist(top_left[None], bottom2, 'sqeuclidean')
rest2 = bottom2[np.argsort(np.max(sqdists,0))[::-1],:]
# Concatenate all these points for the final output
return np.concatenate((sortedtop2c1,rest2),axis =0)
Sample input, output -
In [85]: A
Out[85]:
array([[ 281., 147.],
[ 213., 170.],
[ 239., 242.],
[ 307., 219.]], dtype=float32)
In [86]: sortpts_clockwise(A)
Out[86]:
array([[ 213., 170.],
[ 281., 147.],
[ 307., 219.],
[ 239., 242.]], dtype=float32)