def prepare_image_data(extracted_image_data, output_directory,
                       image_mapping):
    """Prepare and clean image-data from duplicates and other garbage.

    :param: extracted_image_data ([(string, string, list, list) ...],
        ...])): the images and their captions + contexts, ordered
    :param: tex_file (string): the location of the TeX (used for finding the
        associated images; the TeX is assumed to be in the same directory
        as the converted images)
    :param: image_list ([string, string, ...]): a list of the converted
        image file names
    :return extracted_image_data ([(string, string, list, list) ...],
        ...])) again the list of image data cleaned for output
    """
    img_list = {}
    for image, caption, label in extracted_image_data:
        if not image or image == 'ERROR':
            continue
        image_location = get_image_location(
            image,
            output_directory,
            image_mapping.keys()
        )

        if not image_location or not os.path.exists(image_location) or \
                len(image_location) < 3:
            continue

        image_location = os.path.normpath(image_location)
        if image_location in img_list:
            if caption not in img_list[image_location]['captions']:
                img_list[image_location]['captions'].append(caption)
        else:
            img_list[image_location] = dict(
                url=image_location,
                original_url=image_mapping[image_location],
                captions=[caption],
                label=label,
                name=get_name_from_path(image_location, output_directory)
            )
    return img_list.values()
Prepare and clean image-data from duplicates and other garbage.

    :param: extracted_image_data ([(string, string, list, list) ...],
        ...])): the images and their captions + contexts, ordered
    :param: tex_file (string): the location of the TeX (used for finding the
        associated images; the TeX is assumed to be in the same directory
        as the converted images)
    :param: image_list ([string, string, ...]): a list of the converted
        image file names
    :return extracted_image_data ([(string, string, list, list) ...],
        ...])) again the list of image data cleaned for output