def rename(self, force=False): """ Rename a file according to its exif data""" logger.info("Start process for renaming photo %s", self.full_path) # Check if we continue on rename processing regex = re.compile(conf["regex"]["photo_file"], re.IGNORECASE) if force is False and regex.match(self.filename): logger.debug("File %s not renamed as it matches expected regex", self.full_path) return # Get exif data data = exif.load_exif_data(self.full_path) original_date = self.get_date(data) model = self.get_model(data) # Build new filename new_filename = self.build_filename(original_date, model) # Rename if self.full_path != new_filename: os.rename(self.full_path, new_filename) logger.info("Rename photo %s into %s", self.full_path, new_filename) else: logger.debug("File %s has not been renamed", self.full_path)
def test_remove_exif_tag(): # Load all exif data exif_data = exif.load_exif_data( "./test/resources/2013-06-01_10-08-04_Julien.NEF") # Load specific XMP tag exif.remove_exif_data(exif_data, "Xmp.lr.PrivateRTKInfo") exif_hierarchical_subject = exif.get_exif_data(exif_data, "Xmp.lr.PrivateRTKInfo") # Assertions assert exif_hierarchical_subject is None
def test_update_exif_tag(): # Load all exif data exif_data = exif.load_exif_data( "./test/resources/2013-06-01_10-08-04_Julien.NEF") # Load specific XMP tag exif.add_or_update_exif_data(exif_data, "Xmp.lr.PrivateRTKInfo", ["test_value_update"]) result = exif.get_exif_data(exif_data, "Xmp.lr.PrivateRTKInfo") # Assertions assert result.decode("utf-8") == "test_value_update"
def tag(self, tags, force=False): """Apply Exif Data from metadata file""" logger.info("Start process for tagging photo %s", self.full_path) data = exif.load_exif_data(self.full_path) # Extract tags to apply from conf remove_tags = tags["remove"] add_tags = tags["add"] # Remove tags for remove_tag in remove_tags: exif_values = exif.get_exif_data(data, remove_tag['name']) # Remove from existing list, values from configuration exif_values = [] if exif_values is None else exif_values new_values = list((exif for exif in exif_values if exif not in remove_tag['values'])) if new_values is None or len(new_values) == 0: # delete tag logger.debug("Remove tag %s on file %s", new_values, self.full_path) exif.remove_exif_data(data, remove_tag["name"]) elif Counter(exif_values) != Counter(new_values): # update tag data logger.debug("Apply those tags %s on file %s", new_values, self.full_path) exif.add_or_update_exif_data(data, remove_tag["name"], new_values) # Add tags for add_tag in add_tags: exif_values = exif.get_exif_data(data, add_tag['name']) logger.debug("existing tags %s with values %s", add_tag['name'], exif_values) exif_values = [] if exif_values is None else exif_values # Add new values from conf into existing list, without duplicates new_values = list(exif_values) new_values.extend( (x for x in add_tag['values'] if x not in new_values)) if Counter(exif_values) != Counter(new_values): # update tag data logger.debug("Apply those tags %s on file %s", new_values, self.full_path) exif.add_or_update_exif_data(data, add_tag["name"], new_values) exif.write_exif_data(data)
def test_load_exif_tag(): # Load all exif data # exif_data = exif.load_exif_data("./test/resources/2013-06-01_10-08-04_Julien.NEF") exif_data = exif.load_exif_data( "/media/julien/RAID/Images/RAW/2020/2020-09_Theo/2020-10-11_15-03-56_1_D750.NEF" ) original_date = exif.get_exif_data(exif_data, "Exif.Photo.DateTimeOriginal") print(original_date) # Load specific XMP tag exif_hierarchical_subject = exif.get_exif_data( exif_data, "Xmp.lr.hierarchicalSubject") exif_subject = exif.get_exif_data(exif_data, "Xmp.dc.Subject") # Assertions assert isinstance(exif_hierarchical_subject, list) assert "Sorties" in exif_hierarchical_subject assert "Sorties|2013" in exif_hierarchical_subject assert "Sorties|2013|Evenement" in exif_hierarchical_subject assert "Sorties|2013|Evenement|Roland_Garros" in exif_hierarchical_subject assert exif_subject == "Sorties|2013|Evenement|Roland_Garros"