def optimize_png_file(f, o=None):
if isinstance(f, str):<EOL><INDENT>if o is None:<EOL><INDENT>o = f<EOL><DEDENT>else:<EOL><INDENT>assert isinstance(o, str)<EOL><DEDENT>try:<EOL><INDENT>subprocess.check_call(['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', o, f])<EOL><DEDENT>except subprocess.CalledProcessError:<EOL><INDENT>return False<EOL><DEDENT>return True<EOL><DEDENT>if not hasattr(f, '<STR_LIT>'):<EOL><INDENT>raise RuntimeError<EOL><DEDENT>if o is None:<EOL><INDENT>o = f<EOL><DEDENT>else:<EOL><INDENT>if not hasattr(f, '<STR_LIT>'):<EOL><INDENT>raise RuntimeError<EOL><DEDENT><DEDENT>f.seek(<NUM_LIT:0>)<EOL>try:<EOL><INDENT>p = subprocess.Popen(['<STR_LIT>', '<STR_LIT:->'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)<EOL>r = p.communicate(f.read())[<NUM_LIT:0>]<EOL><DEDENT>except IOError:<EOL><INDENT>r = None<EOL><DEDENT>if r:<EOL><INDENT>truncate_file(o)<EOL>o.write(r)<EOL>return True<EOL><DEDENT>return False<EOL>
Use pngquant for optimize PNG-image.
f - path to input image file or file-object.
o - path to output image file or file-object for save result.
NOTICE: f and o can not be of different type