def append_meta_data(data_to_parse, directory): print "-" * 50 print "Copying Files and Appending MetaData" print "-" * 50 new_data = "" for item in data_to_parse.split("\n"): if "Photo name:" in item: new_data = item.strip().replace("Photo name: ", "") img_name = directory + "_new-metadata" + "\\" + new_data metadata = pyexiv2.ImageMetadata(img_name) metadata.read() if "Photo title:" in item: new_data = item.strip().replace("Photo title: ", "") key = "Xmp.dc.title" value = new_data metadata[key] = pyexiv2.XmpTag(key, value) if "Photo caption:" in item: new_data = item.strip().replace("Photo caption: ", "") + "\n\n" if "For more see" in item: new_data += item.strip().replace(".html", ".html?cmpid=CampaignID") key_caption = "Iptc.Application2.Caption" value_caption = [new_data] metadata[key_caption] = pyexiv2.IptcTag(key_caption, value_caption) metadata.write() print "-" * 50 print "Completed" print "-" * 50
def set_metadata_in_file(filename, flickr_photo): ''' set_metadata_in_file ''' file_metadata = pyexiv2.ImageMetadata(filename) file_metadata.read() flickr_meta = {} for each_taxonomy in flickr_photo.meta: key = each_taxonomy['metadata'] file_metadata[key] = pyexiv2.IptcTag(key, each_taxonomy['value']) file_metadata.write()
def _write_iptc_tags(new_file_url: str, tag_data: dict) -> bool: """ method to write IPTC tags to image :param new_file_url: filename of target image :param tag_data: image data: in form: {'iptc_key': iptc key, 'tags': ['tag 1', 'tag 2']} :return: True | False """ try: iptc_key = tag_data['iptc_key'] if iptc_key: tags = tag_data['tags'] print(f'Tags to write: {tags}') meta = pyexiv2.ImageMetadata(new_file_url) meta.read() meta[iptc_key] = pyexiv2.IptcTag(iptc_key, tags) meta.write() print('No tag to write!') except (TypeError, Exception) as e: print(f'An error occurred in write_iptc_tags: {e}') return False
def add_exif(image_name, tags): try: metadata = pyexiv2.ImageMetadata(image_name) metadata.read() except EnvironmentError: sys.stderr.write("Error reading metadata for image %s\n" % image_name) return KW_KEY = 'Iptc.Application2.Keywords' if '-' in options.exif: # remove all tags if KW_KEY in metadata.iptc_keys: del metadata[KW_KEY] else: # add tags if KW_KEY in metadata.iptc_keys: tags |= set(metadata[KW_KEY].value) tags = list(tag.strip().lower() for tag in tags | options.exif if tag) metadata[KW_KEY] = pyexiv2.IptcTag(KW_KEY, tags) try: metadata.write() except EnvironmentError: sys.stderr.write("Writing metadata failed for tags: %s in: %s\n" % (tags, image_name))
def write_iptc_tags(path, filename, tag_data): """ method to write IPTC tags to image :param path: path to target image :param filename: filename of target image :param tag_data: original image data: in form: {'path': /path/to/original/image, 'filename': filename, 'iptc_key': key, 'tags': ['tag 1', 'tag 2']} :return: True | False """ try: iptc_key = tag_data['iptc_key'] tags = tag_data['tags'] url = os.path.join(path, filename) meta = pyexiv2.ImageMetadata(os.path.join(url)) meta.read() meta[iptc_key] = pyexiv2.IptcTag(iptc_key, tags) meta.write() print('Tags successfully written!') return True except (TypeError, Exception) as e: print(e) return False
def _write_iptc_tags(new_file_url: str, tag_data: dict) -> bool: """ method to write IPTC tags to image :param new_file_url: filename of target image :param tag_data: image data: in form: {'iptc_key': iptc key, 'tags': ['tag 1', 'tag 2']} :return: True | False """ try: iptc_key = tag_data['iptc_key'] if iptc_key and tag_data['tags']: tags = tag_data['tags'] logger.info(f'Tags to write: {tags}') meta = pyexiv2.ImageMetadata(new_file_url) meta.read() meta[iptc_key] = pyexiv2.IptcTag(iptc_key, tags) meta.write() else: logger.warning( 'NO TAGS WERE SUBMITTED TO WRITE, SO ASSUMING ONLY 1 TAG EXISTED & THE INTENTION WAS TO WRITE AN EMPTY TAG SET, WITH THE EFFECT OF DELETING IT') ProcessImages.delete_iptc_tags(new_file_url) # delete the tag logger.info('No more tags to write!') except (TypeError, Exception) as e: logger.error(f'An error occurred in write_iptc_tags: {e}') return False
def write_tags(image, key, tags): """Write each tags into the iptc key inside an image. Tags must be a list""" image[key] = pyexiv2.IptcTag(key, tags) image.write()
def set_tags(file, tags): metadata = pyexiv2.ImageMetadata(file) metadata.read() key = 'Iptc.Application2.Keywords' metadata[key] = pyexiv2.IptcTag(key, tags) metadata.write()