<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def prepare_image_data(extracted_image_data, output_directory, image_mapping):
 """Prepare and clean image-data from duplicates and other garbage. :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) image file names """

    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()