'Process and rewrite image URIs.'

def process_images(self, docname, doctree):

    docdir = path.dirname(self.doc2path(docname, base=None))
    for node in doctree.traverse(nodes.image):
        node['candidates'] = candidates = {}
        imguri = node['uri']
        if (imguri.find('://') != (-1)):
            self.warn(docname, ('nonlocal    image    URI    found:    %s' % imguri), node.line)
            candidates['?'] = imguri
            continue
        if (imguri.startswith('/') or imguri.startswith(os.sep)):
            imgpath = path.normpath(imguri[1:])
        else:
            imgpath = path.normpath(path.join(docdir, imguri))
        node['uri'] = imgpath
        if imgpath.endswith((os.extsep + '*')):
            for filename in glob(path.join(self.srcdir, imgpath)):
                new_imgpath = relative_path(self.srcdir, filename)
                if filename.lower().endswith('.pdf'):
                    candidates['application/pdf'] = new_imgpath
                elif filename.lower().endswith('.svg'):
                    candidates['image/svg+xml'] = new_imgpath
                else:
                    try:
                        f = open(filename, 'rb')
                        try:
                            imgtype = imghdr.what(f)
                        finally:
                            f.close()
                    except (OSError, IOError):
                        self.warn(docname, ('image    file    %s    not    readable' % filename))
                    if imgtype:
                        candidates[('image/' + imgtype)] = new_imgpath
        else:
            candidates['*'] = imgpath
        for imgpath in candidates.itervalues():
            self.dependencies.setdefault(docname, set()).add(imgpath)
            if (not os.access(path.join(self.srcdir, imgpath), os.R_OK)):
                self.warn(docname, ('image    file    not    readable:    %s' % imgpath), node.line)
                continue
            self.images.add_file(docname, imgpath)
