def optimize_media(file_, max_size, formats):
if not PIL:<EOL><INDENT>msg = ("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>raise RuntimeError(msg)<EOL><DEDENT>img = PIL.Image.open(file_)<EOL>ratio = max(hw / max_hw for hw, max_hw in zip(img.size, max_size))<EOL>if ratio > <NUM_LIT:1>:<EOL><INDENT>size = tuple(int(hw // ratio) for hw in img.size)<EOL>img = img.resize(size, PIL.Image.ANTIALIAS)<EOL><DEDENT>media = convert(img, formats)<EOL>if not hasattr(file_, '<STR_LIT>'):<EOL><INDENT>img.close()<EOL><DEDENT>return media<EOL>
Optimize an image
Resize the picture to the ``max_size``, defaulting to the large
photo size of Twitter in :meth:`PeonyClient.upload_media` when
used with the ``optimize_media`` argument.
Parameters
----------
file_ : file object
    the file object of an image
max_size : :obj:`tuple` or :obj:`list` of :obj:`int`
    a tuple in the format (width, height) which is maximum size of
    the picture returned by this function
formats : :obj`list` or :obj:`tuple` of :obj:`dict`
    a list of all the formats to convert the picture to
Returns
-------
file
    The smallest file created in this function