« Back to Index

[Calculate Aspect Ratio]

View original Gist on GitHub

Tags: #aspect #ratio #python

aspect_ratio.py

def calculate_aspect(width: int, height: int) -> str:
    def gcd(a, b):
        """The GCD (greatest common divisor) is the highest number that evenly divides both width and height."""
        return a if b == 0 else gcd(b, a % b)

    r = gcd(width, height)
    x = int(width / r)
    y = int(height / r)

    return f"{x}:{y}"
    
calculate_aspect(1920, 1080) # '16:9'