def pad_image_to_aspect_ratio(img, max_aspect_ratio=5):
    """
    Pad an image to ensure its aspect ratio (width/height or height/width) is less than the given value.

    Parameters:
        img (PIL.Image): The input PIL Image object.
        max_aspect_ratio (float): The maximum allowed aspect ratio.

    Returns:
        PIL.Image: The padded image.
    """
    width, height = img.size
    # Calculate the required minimum dimensions to satisfy the aspect ratio constraint
    if width > height * max_aspect_ratio:
        # Width is too large, pad height
        new_height = int(width / max_aspect_ratio + 1)
        new_width = width
    elif height > width * max_aspect_ratio:
        # Height is too large, pad width
        new_width = int(height / max_aspect_ratio + 1)
        new_height = height
    else:
        # Aspect ratio is already less than or equal to max_aspect_ratio
        return img

    # Calculate the padding amounts
    pad_width = (new_width - width)
    pad_height = (new_height - height)

    # Pad the image symmetrically
    padding = (pad_width // 2, pad_height // 2, pad_width - pad_width // 2, pad_height - pad_height // 2)
    padded_img = ImageOps.expand(img, padding, fill=(0, 0, 0))  # Fill color is black (0, 0, 0) by default

    return padded_img

    Pad an image to ensure its aspect ratio (width/height or height/width) is less than the given value.

    Parameters:
        img (PIL.Image): The input PIL Image object.
        max_aspect_ratio (float): The maximum allowed aspect ratio.

    Returns:
        PIL.Image: The padded image.
    
vlmeval/vlm/emu.py