Skip to content

Instantly share code, notes, and snippets.

@junmakii
Created September 15, 2017 02:23
Show Gist options
  • Select an option

  • Save junmakii/72d42411492d5eff8031d122d15f9755 to your computer and use it in GitHub Desktop.

Select an option

Save junmakii/72d42411492d5eff8031d122d15f9755 to your computer and use it in GitHub Desktop.
"""A image utility function to keep image aspect ratio.
"""
def get_size(im_size, size):
"""
:rtype: (int, int)
>>> get_size((360, 100), (360, 180))
(648, 180)
>>> get_size((100, 360), (360, 180))
(360, 1296)
>>> get_size((512, 512), (640, 360))
(640, 640)
"""
if im_size[0] == im_size[1]:
return (max(size), max(size))
elif im_size[0] > im_size[1]:
width = size[1] * im_size[0] / im_size[1]
return (width, size[1])
else:
height = size[0] * im_size[1] / im_size[0]
return (size[0], height)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment