def convert(img, formats):
media = None<EOL>min_size = <NUM_LIT:0><EOL>for kwargs in formats:<EOL><INDENT>f = io.BytesIO()<EOL>if img.mode == "<STR_LIT>" and kwargs['<STR_LIT>'] != "<STR_LIT>":<EOL><INDENT>if min_size < <NUM_LIT:5> * <NUM_LIT>**<NUM_LIT:2>:<EOL><INDENT>continue<EOL><DEDENT>else:<EOL><INDENT>img.convert('<STR_LIT>')<EOL><DEDENT><DEDENT>img.save(f, **kwargs)<EOL>size = f.tell()<EOL>if media is None or size < min_size:<EOL><INDENT>if media is not None:<EOL><INDENT>media.close()<EOL><DEDENT>media = f<EOL>min_size = size<EOL><DEDENT>else:<EOL><INDENT>f.close()<EOL><DEDENT><DEDENT>return media<EOL>
Convert the image to all the formats specified
Parameters
----------
img : PIL.Image.Image
    The image to convert
formats : list
    List of all the formats to use
Returns
-------
io.BytesIO
    A file object containing the converted image