Take a raw image name + directory and return the location of image.

    :param: image (string): the name of the raw image from the TeX
    :param: sdir (string): the directory where everything was unzipped to
    :param: image_list ([string, string, ...]): the list of images that
        were extracted from the tarball and possibly converted

    :return: converted_image (string): the full path to the (possibly
        converted) image file

def get_image_location(image, sdir, image_list, recurred=False):
    """Take a raw image name + directory and return the location of image.

    :param: image (string): the name of the raw image from the TeX
    :param: sdir (string): the directory where everything was unzipped to
    :param: image_list ([string, string, ...]): the list of images that
        were extracted from the tarball and possibly converted

    :return: converted_image (string): the full path to the (possibly
        converted) image file
    """
    if isinstance(image, list):
        # image is a list, not good
        return None

    image = image.encode('utf-8', 'ignore')
    image = image.strip()

    figure_or_file = '(figure=|file=)'
    figure_or_file_in_image = re.findall(figure_or_file, image)
    if len(figure_or_file_in_image) > 0:
        image = image.replace(figure_or_file_in_image[0], '')

    includegraphics = r'\\includegraphics{(.+)}'
    includegraphics_in_image = re.findall(includegraphics, image)
    if len(includegraphics_in_image) > 0:
        image = includegraphics_in_image[0]

    image = image.strip()

    some_kind_of_tag = '\\\\\\w+ '

    if image.startswith('./'):
        image = image[2:]
    if re.match(some_kind_of_tag, image):
        image = image[len(image.split(' ')[0]) + 1:]
    if image.startswith('='):
        image = image[1:]

    if len(image) == 1:
        return None

    image = image.strip()
    converted_image_should_be = get_converted_image_name(image)

    if image_list is None:
        image_list = os.listdir(sdir)

    for png_image in image_list:
        png_image_rel = os.path.relpath(png_image, start=sdir)
        if converted_image_should_be == png_image_rel:
            return png_image

    # maybe it's in a subfolder (TeX just understands that)
    for prefix in ['eps', 'fig', 'figs', 'figures', 'figs', 'images']:
        if os.path.isdir(os.path.join(sdir, prefix)):
            image_list = os.listdir(os.path.join(sdir, prefix))
            for png_image in image_list:
                if converted_image_should_be == png_image:
                    return os.path.join(sdir, prefix, png_image)

    # maybe it is actually just loose.
    for png_image in os.listdir(sdir):
        if os.path.split(converted_image_should_be)[-1] == png_image:
            return converted_image_should_be
        if os.path.isdir(os.path.join(sdir, png_image)):
            # try that, too!  we just do two levels, because that's all that's
            # reasonable..
            sub_dir = os.path.join(sdir, png_image)
            for sub_dir_file in os.listdir(sub_dir):
                if os.path.split(converted_image_should_be)[-1] == sub_dir_file:  # noqa
                    return os.path.join(sub_dir, converted_image_should_be)

    # maybe it's actually up a directory or two: this happens in nested
    # tarballs where the TeX is stored in a different directory from the images
    for png_image in os.listdir(os.path.split(sdir)[0]):
        if os.path.split(converted_image_should_be)[-1] == png_image:
            return converted_image_should_be
    for png_image in os.listdir(os.path.split(os.path.split(sdir)[0])[0]):
        if os.path.split(converted_image_should_be)[-1] == png_image:
            return converted_image_should_be

    if recurred:
        return None

    # agh, this calls for drastic measures
    for piece in image.split(' '):
        res = get_image_location(piece, sdir, image_list, recurred=True)
        if res is not None:
            return res

    for piece in image.split(','):
        res = get_image_location(piece, sdir, image_list, recurred=True)
        if res is not None:
            return res

    for piece in image.split('='):
        res = get_image_location(piece, sdir, image_list, recurred=True)
        if res is not None:
            return res

    return None