def dimensionsKeepAspect(targetWidth, targetHeight, oldWidth, oldHeight):
    """
    Gives resizing dimensions to keep an image within (targetWidth, targetHeight)
    while preserving the original aspect ratio. Does not upsize iamges smaller
    than the target dimensions.
    """
    if (oldWidth < targetWidth) and (oldHeight < targetHeight):
        return (int(oldWidth), int(oldHeight))
    oldAspect = oldWidth/float(oldHeight)
    newAspect = targetWidth/float(targetHeight)
    if oldAspect > newAspect:
        newWidth = targetWidth
        newHeight = targetWidth/oldAspect
        return (int(newWidth), int(newHeight))
    elif oldAspect < newAspect:
        newHeight = targetHeight
        newWidth = targetHeight*oldAspect
        return (int(newWidth), int(newHeight))
    elif oldAspect == newAspect:
        return (int(targetWidth), int(targetHeight))