Skip to content

Instantly share code, notes, and snippets.

@stuaxo
Last active December 20, 2015 20:49
Show Gist options
  • Save stuaxo/6193014 to your computer and use it in GitHub Desktop.
Save stuaxo/6193014 to your computer and use it in GitHub Desktop.
Find the largest rectangle of {aspect ratio} that can fit into the given dimensions.
def get_aspect(width, height):
""" :return aspect ratio """
return float(width) / float(height)
def get_ideal_size(width, height, ideal_aspect):
"""
:return the size of the largest rectangle with an aspect
ratio of 'ideal_aspect' that will fit into width x height.
"""
aspect = get_aspect(width, height)
if ideal_aspect < aspect:
resized = height * ideal_aspect, height
else:
resized = width, width / ideal_aspect
return resized
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment