Finding length and width with python

If I wanted to calculate the length and width of an object by placing lines at the 4 ends, how should I start? I’m taking into consideration, always different size and different objects and different backgrounds