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 pngSetRating(p_filename, p_num): f_metadata = pyexiv2.ImageMetadata(p_filename) f_metadata.read() f_key = 'Xmp.xmp.Rating' f_value = p_num f_metadata[f_key] = pyexiv2.XmpTag(f_key, f_value) f_metadata.write() return
def pngSetCreator(p_filename, p_list): f_metadata = pyexiv2.ImageMetadata(p_filename) f_metadata.read() f_key = 'Xmp.dc.creator' f_value = p_list f_metadata[f_key] = pyexiv2.XmpTag(f_key, f_value) f_metadata.write() return
def pngSetDescription(p_filename, p_string): f_metadata = pyexiv2.ImageMetadata(p_filename) f_metadata.read() f_key = 'Xmp.dc.description' f_value = p_string f_metadata[f_key] = pyexiv2.XmpTag(f_key, f_value) f_metadata.write() return
def pngSetKeywords(p_filename, p_list): f_metadata = pyexiv2.ImageMetadata(p_filename) f_metadata.read() f_key = 'Xmp.xmp.Label' f_value = MetadataManager.cleanList2cleanStr(p_list) f_metadata[f_key] = pyexiv2.XmpTag(f_key, f_value) f_metadata.write() return
def write_geo_tag(img_path, lat, lon, alt_abs, hdg=None, roll=None, pitch=None, yaw=None): """Writes geotags to an image Arguments: img_path {str} -- Path to image lat {float} -- latitude, in decimal degrees lon {float} -- longitude, in decimal degrees alt_abs {float} -- absolute altitude, in metres Keyword Arguments: hdg {float} -- Heading, in degrees (default: {None}) roll {float} -- Roll, in degrees (default: {None}) pitch {float} -- Pitch, in degrees (default: {None}) yaw {float} -- Yaw, in degrees (default: {None}) Raises: ValueError -- if image is not a JPEG or MPO """ # Only JPEG and MPO have metadata img = Image.open(img_path) if not (img.format == 'JPEG' or img.format == "MPO"): raise ValueError('Image is not a JPEG or MPO') img.close() metadata = pyexiv2.ImageMetadata(img_path) metadata.read() # Add standard Exif Tags tags = ['Exif.GPSInfo.GPSLatitude', 'Exif.GPSInfo.GPSLatitudeRef', 'Exif.GPSInfo.GPSLongitude', 'Exif.GPSInfo.GPSLongitudeRef', 'Exif.GPSInfo.GPSAltitude', 'Exif.GPSInfo.GPSAltitudeRef'] values = [coord_dec_to_dms(lat), _get_lat_ref(lat), coord_dec_to_dms(lon), _get_lon_ref(lon), Fraction(int(alt_abs*1e7), int(1e7)), _get_alt_ref(alt_abs)] for (tag, value) in zip(tags, values): metadata[tag] = pyexiv2.ExifTag(tag, value) if hdg is not None: tag = 'Exif.GPSInfo.GPSImgDirection' metadata[tag] = pyexiv2.ExifTag(tag, Fraction(int(_check_angle(hdg) * 100), 100)) # Add roll, pitch, yaw as custom Xmp tags tags = ['Xmp.Attitude.Roll', 'Xmp.Attitude.Pitch', 'Xmp.Attitude.Yaw'] values = [roll, pitch, yaw] attitudes = [roll, pitch, yaw] for (attitude, tag, value) in zip(attitudes, tags, values): if attitude is not None: metadata[tag] = pyexiv2.XmpTag(tag, str(_check_angle(value))) metadata.write()
def set_datetime(self, new_datetime): """ Update EXIF/XMP tags as needed and write metadata. """ # TODO: Timezone? xmp_datetime = new_datetime.strftime('%Y-%m-%dT%H:%M:%S') exif_datetime = new_datetime.strftime('%Y:%m:%d %H:%M:%S') logger.debug("XMP: {} : EXIF: {}".format(xmp_datetime, exif_datetime)) # Shotwell will use Xmp.xmp.CreateDate if set, so it must be updated. if 'Xmp.xmp.CreateDate' in self['metadata'].keys(): self.img_md['Xmp.xmp.CreateDate'] = pyexiv2.XmpTag( 'Xmp.xmp.CreateDate', new_datetime) if 'Exif.Photo.DateTimeOriginal' in self['metadata'].keys(): self.img_md['Exif.Photo.DateTimeOriginal'] = pyexiv2.ExifTag( 'Exif.Photo.DateTimeOriginal', new_datetime) self.img_md['Exif.Image.DateTime'] = pyexiv2.ExifTag( 'Exif.Image.DateTime', new_datetime) try: self.img_md.write() except Exception as e: logger.error(e)
elif orientation == 5: # Horizontal Mirror + Rotation 270° mirror = im.transpose(Image.FLIP_TOP_BOTTOM).transpose(Image.ROTATE_270) elif orientation == 6: # Rotation 270° mirror = im.transpose(Image.ROTATE_270) elif orientation == 7: # Vertical Mirror + Rotation 270° mirror = im.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_270) elif orientation == 8: # Rotation 90° mirror = im.transpose(Image.ROTATE_90) # No more Orientation information try: image['Exif.Image.Orientation'] = pyexiv2.XmpTag('Exif.Image.Orientation', 1) except KeyError: pass else: # No EXIF information, the user has to do it mirror = im.copy() mirror.save(outfilename + extension) if extension.lower() in [".jpg", ".jpeg"]: img_grand = pyexiv2.ImageMetadata(outfilename + extension) img_grand.read() #try: # copy the metadata # for k in image.exif_keys: # img_grand[k] = image[k] #except TypeError: