def spawn_viewer(viewer, img, filename, grace):
tempdir = tempfile.mkdtemp(prefix='<STR_LIT>')<EOL>try:<EOL><INDENT>imgfile = os.path.join(tempdir, filename)<EOL>img.save(imgfile)<EOL>started = time.time()<EOL>subprocess.call([viewer, imgfile])<EOL>elapsed = time.time() - started<EOL>if elapsed < grace:<EOL><INDENT>time.sleep(grace - elapsed)<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>shutil.rmtree(tempdir)<EOL><DEDENT>
Launch an external program to view an image.

    ``img`` is an Image object.

    ``viewer`` is a command name.  Arguments are not allowed; exactly one
    argument will be passed: the name of the image file.

    ``filename`` is the suggested filename for a temporary file.

    ``grace`` is the number of seconds to wait after spawning the viewer
    before removing the temporary file.  Useful if your viewer forks
    into background before it opens the file.