Figured how to acces class objects. Moved write_exif to own Exif class to compile GPS coords and write them all.
		
			
				
	
	
		
			92 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			92 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| #!/usr/bin/env python
 | |
| # -*- coding: utf-8 -*-
 | |
| 
 | |
| ''' Classes used by main program. '''
 | |
| 
 | |
| from datetime import datetime
 | |
| import os
 | |
| import shutil
 | |
| import pyexiv2
 | |
| 
 | |
| class Radiation:
 | |
|     ''' Handles CSV processing. '''
 | |
|     def __init__(self, timestamp, radiation, local_timezone, si_factor):
 | |
|         self.timestamp = self._time_conversion(timestamp, local_timezone)
 | |
|         self.radiation = self._radiation_conversion(radiation, si_factor)
 | |
| 
 | |
|     def __repr__(self):
 | |
|         return '%s %f µS/h' % (str(self.timestamp), self.radiation)
 | |
| 
 | |
|     def _time_conversion(self, timestamp, local_timezone):
 | |
|         csv_naive_time = datetime.fromisoformat(timestamp)
 | |
|         # Set timezone
 | |
|         csv_aware_time = csv_naive_time.astimezone(local_timezone)
 | |
|         return csv_aware_time
 | |
| 
 | |
|     def _radiation_conversion(self, radiation, si_factor):
 | |
|         # Convert CP/M to µS/h using si_factor
 | |
|         radiation = round(float(radiation) * si_factor, 2)
 | |
|         return radiation
 | |
| 
 | |
| class Photo:
 | |
|     ''' Reads and writes Exif metadata. '''
 | |
|     def __init__(self, photo, local_timezone, dest_dir, dry_run):
 | |
|         self.get_date = self._get_creation_date(photo, local_timezone)
 | |
|         self.get_target_photo = self._copy_photo(photo, dest_dir, dry_run)
 | |
| 
 | |
|     def __repr__(self):
 | |
|         return 'Photo: %s Creation Date: %s' % (str(self.get_target_photo), str(self.get_date))
 | |
| 
 | |
|     def _copy_photo(self, photo, dest_dir, dry_run):
 | |
|         # Determine where to work on photo and copy it there if needed.
 | |
|         # Get image file name out of path
 | |
|         photo_basename = os.path.basename(photo)
 | |
|         # be os aware and use the correct directory delimiter for destfile
 | |
|         dest_photo = os.path.join(dest_dir, photo_basename)
 | |
| 
 | |
|         # Copy photo to dest_dir and return its (new) filename
 | |
|         # if not in dry_run mode or if dest_dir is different from src_dir.
 | |
|         if dry_run is True:
 | |
|             return photo
 | |
|         if dest_dir != '.':
 | |
|             shutil.copy(photo, dest_photo)
 | |
|         return dest_photo
 | |
| 
 | |
|     def _get_creation_date(self, photo, local_timezone):
 | |
|         # Load Exif data from photo
 | |
|         metadata = pyexiv2.ImageMetadata(photo)
 | |
|         metadata.read()
 | |
|         print(metadata)
 | |
|         date = metadata['Exif.Photo.DateTimeOriginal']
 | |
|         # date.value creates datetime object in pic_naive_time
 | |
|         pic_naive_time = date.value
 | |
|         # Set timezone
 | |
|         pic_aware_time = pic_naive_time.astimezone(local_timezone)
 | |
|         return pic_aware_time
 | |
| 
 | |
| class Exif:
 | |
|     ''' Compiles and writes Exif-Tags from given values. '''
 | |
|     def write_exif(photo, radiation, latitude, longitude, dry_run):
 | |
| 
 | |
|         ''' UNTESTED ! '''
 | |
| 
 | |
|         metadata = pyexiv2.ImageMetadata(photo)
 | |
|         metadata.read()
 | |
|         # Set new UserComment
 | |
|         new_comment = 'Radiation ☢ ' + str(radiation) + ' µS/h'
 | |
|         # Exif tags to write
 | |
|         keys = ['Exif.Photo.UserComment', 'Exif.Photo.latitude', 'Exif.Photo.longitude']
 | |
|         # Values to write
 | |
|         values = [new_comment, latitude, longitude]
 | |
|         
 | |
|         # Create metadata object with all data to write
 | |
|         for key, value in zip(keys, values):
 | |
|             # Only create object if there is anything to fill with
 | |
|             if value is not None:
 | |
|                 metadata[key] = pyexiv2.ExifTag(key, value)
 | |
| 
 | |
|         # Write Exif tags to file, if not in dry-run mode
 | |
|         if dry_run is not True:
 | |
|             metadata.write()
 | |
|         return new_comment
 |