image_utils

Utility Functions to work with images. Includes all stable utility functions.

Image metadata


source

get_date_from_file_name

 get_date_from_file_name (path2file:pathlib.Path, date_pattern:str=None)

Retrieve the date from the file name.

Returns the date encoded in the file name (as per date_pattern) as a datetime. Default date pattern is YY-MM-DD, i.g. regex ^(-(-(*

returns: date in datetime format, if found. False if not found or if file does not exist

Type Default Details
path2file Path Path to the file
date_pattern str None regex pattern for the date, if None, default date pattern is YY-MM-DD
Returns dt.datetime
p2img = Path('../data/img/22-06-11_IMG_512px.jpg')
get_date_from_file_name(path2file=p2img)
datetime.datetime(2022, 6, 11, 0, 0)

source

date_is_within_year

 date_is_within_year (date, year)

True if the passed date (datetime) is within year, False otherwise


source

exif2dt

 exif2dt (exif_d)

Transform a date in bytes format from EXIF into datetime format

exif_d: date in exif format, i.e. bytes. E.g. b”2018:11:21” returns: the date in datetime or False if no date is detected or not well formatted


source

add_missing_dates_to_exif

 add_missing_dates_to_exif (path2folder:pathlib.Path, year:int=None,
                            maxhours:int=24,
                            do_not_update_exif:bool=False,
                            verbose:bool=False)

Add missing EXIF original and digitized dates, based on file creation or date in file name.

Type Default Details
path2folder Path Path to the folder holding all jpg photos to handle
year int None year used to filter all dates
maxhours int 24 maximum acceptable difference (in hours) between exif dates and file dates
do_not_update_exif bool False when Trud, prevent updating the exif file; used in debugging or testing
verbose bool False when True, print original, updated and retrieved updated EXIF info

Logic of the function:

In order to better control the data changes and avoid mistaken exif updates, the process is done on a year by year basis, i.e. a specific year needs to be passed to the function and only dates within the passed year will be updated. All other dates will be disregarded.

  1. Retrieve EXIF info from image
  2. When there is no EXIF.DatetimeOriginal in the image EXIF:
    • use date from file name if exists, else
    • use date from creation or modification, whichever is earlier
  3. When there is an EXIF.DatetimeOriginal, compare with date from file, if any:
    • if difference < maxhours, do nothing
    • if difference >= maxhours, use date from filename
  4. If the date extracted from file name or file creation/modification date is not in passed year, skipped any change