Skip to content

Instantly share code, notes, and snippets.

@TheSkallywag
Forked from Integralist/aspect_ratio.py
Created July 6, 2021 20:16
Show Gist options
  • Save TheSkallywag/e0a6121f394e11acd4411618323e983e to your computer and use it in GitHub Desktop.
Save TheSkallywag/e0a6121f394e11acd4411618323e983e to your computer and use it in GitHub Desktop.
[Calculate Aspect Ratio] #aspect #ratio #python
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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment