def test_value_synced_with_iptc(self): # Create an image. file_descriptor, file_path = tempfile.mkstemp(suffix='.jpg') os.close(file_descriptor) Image.new('RGB', (1, 1)).save(file_path, 'JPEG') metadata = pyexiv2.Image(file_path) metadata.readMetadata() # Add string metadata. test_description = 'Test file with description' metadata['Iptc.Application2.Caption'] = test_description metadata.writeMetadata() metadata = pyexiv2.Image(file_path) metadata.readMetadata() self.assertEqual(metadata['Iptc.Application2.Caption'], 'Test file with description') # Test synchronization status of string metadata. self.assertTrue(value_synced_with_iptc(test_description, metadata, 'Iptc.Application2.Caption')) test_description = 'New, different test description' self.assertFalse(value_synced_with_iptc(test_description, metadata, 'Iptc.Application2.Caption')) # Add iterable metadata. test_keywords = ['IPTC', 'test', 'keywords'] metadata['Iptc.Application2.Keywords'] = test_keywords metadata.writeMetadata() self.assertEqual(metadata['Iptc.Application2.Keywords'], ('IPTC', 'test', 'keywords')) # Test synchronization status of iterable metadata. self.assertTrue(value_synced_with_iptc(test_keywords, metadata, 'Iptc.Application2.Keywords')) test_keywords.append('modified') self.assertFalse(value_synced_with_iptc(test_keywords, metadata, 'Iptc.Application2.Keywords')) # Test synchronization of nonexistent/empty iterable metadata. _del_img_key(metadata, 'Iptc.Application2.Keywords') metadata.writeMetadata() self.assertFalse('Iptc.Application2.Keywords' in metadata.iptcKeys()) empty_list = [] self.assertTrue(value_synced_with_iptc(empty_list, metadata, 'Iptc.Application2.Keywords')) none_argument = None self.assertTrue(value_synced_with_iptc(none_argument, metadata, 'Iptc.Application2.Keywords')) # Test synchronization of iterables that are sorted differently. test_keywords = ['c', 'a', 'b'] metadata['Iptc.Application2.Keywords'] = test_keywords metadata.writeMetadata() test_keywords.sort() self.assertEqual(test_keywords, ['a', 'b', 'c']) self.assertTrue(value_synced_with_iptc(test_keywords, metadata, 'Iptc.Application2.Keywords')) # Test synchronization of iterables of length one. test_keywords = ['a'] try: metadata['Iptc.Application2.Keywords'] = test_keywords except TypeError: pass metadata.writeMetadata() metadata = pyexiv2.Image(file_path) metadata.readMetadata() self.assertTrue(value_synced_with_iptc(test_keywords, metadata, 'Iptc.Application2.Keywords')) # Test synchronization of empty/nonexistent string metadata. empty_string = "" _del_img_key(metadata, 'Iptc.Application2.Caption') self.assertTrue(value_synced_with_iptc(empty_string, metadata, 'Iptc.Application2.Caption')) # Clean up. os.remove(file_path)
def sync_metadata_from_file(self, commit=True): """\ Synchronizes the image metadata from the filesystem to the object. This reads certain properties of the object from Exif and/or IPTC tags in the image file itself, allowing the information to be portable from outside of this application. Metadata is only actually written to the database if the values do not match up, however. Returns True if metadata needed to be written to the database; False otherwise. """ if not self.metadata_sync_enabled: return False try: image_metadata = pyexiv2.Image(self.image.path) image_metadata.readMetadata() except IOError: self.metadata_sync_enabled = False self.save() return False mod_instance = False # whether or not database needs written to # sync description mod_attr = not value_synced_with_exif_and_iptc( self.description, image_metadata, 'Exif.Image.ImageDescription', 'Iptc.Application2.Caption') mod_instance = mod_attr or mod_instance if mod_attr: value = read_value_from_exif_and_iptc(image_metadata, 'Exif.Image.ImageDescription', 'Iptc.Application2.Caption') if value is None: value = str() self.description = value # sync artist mod_attr = not value_synced_with_exif_and_iptc( self.artist, image_metadata, 'Exif.Image.Artist', 'Iptc.Application2.Byline') mod_instance = mod_attr or mod_instance if mod_attr: value = read_value_from_exif_and_iptc(image_metadata, 'Exif.Image.Artist', 'Iptc.Application2.Byline') if value is None: value = str() self.artist = value # sync country mod_attr = not value_synced_with_iptc(self.country, image_metadata, 'Iptc.Application2.CountryName') mod_instance = mod_attr or mod_instance if ('Iptc.Application2.CountryName' in image_metadata.iptcKeys() and mod_attr): value = image_metadata['Iptc.Application2.CountryName'] if value is None: value = str() self.country = value # sync province_state mod_attr = not value_synced_with_iptc( self.province_state, image_metadata, 'Iptc.Application2.ProvinceState') mod_instance = mod_attr or mod_instance if ('Iptc.Application2.ProvinceState' in image_metadata.iptcKeys() and mod_attr): value = image_metadata['Iptc.Application2.ProvinceState'] if value is None: value = str() self.province_state = value # sync city mod_attr = not value_synced_with_iptc(self.city, image_metadata, 'Iptc.Application2.City') mod_instance = mod_attr or mod_instance if 'Iptc.Application2.City' in image_metadata.iptcKeys() and mod_attr: value = image_metadata['Iptc.Application2.City'] if value is None: value = str() self.city = value # sync location mod_attr = not value_synced_with_iptc(self.location, image_metadata, 'Iptc.Application2.SubLocation') mod_instance = mod_attr or mod_instance if ('Iptc.Application2.SubLocation' in image_metadata.iptcKeys() and mod_attr): value = image_metadata['Iptc.Application2.SubLocation'] if value is None: value = str() self.location = value # sync time_created mod_attr = not datetime_synced_with_exif_and_iptc( self.time_created, image_metadata, 'Exif.Photo.DateTimeOriginal', 'Iptc.Application2.DateCreated', 'Iptc.Application2.TimeCreated') mod_instance = mod_attr or mod_instance if mod_attr: self.time_created = read_datetime_from_exif_and_iptc( image_metadata, 'Exif.Photo.DateTimeOriginal', 'Iptc.Application2.DateCreated', 'Iptc.Application2.TimeCreated') # sync keywords mod_attr = not value_synced_with_iptc(self.keyword_list, image_metadata, 'Iptc.Application2.Keywords') mod_instance = mod_attr or mod_instance if ('Iptc.Application2.Keywords' in image_metadata.iptcKeys() and mod_attr): self.keyword_list = image_metadata['Iptc.Application2.Keywords'] if mod_instance and commit: self.save() return mod_instance