Thursday, 29 October 2020

Add "Date Taken" Exif/XMP information to TIF file using Python

Using the following Python code on Windows 10, I am trying to edit the EXIF info for "Date Taken" on a tif image file created with Nikon Scan 4.0.3 (software from around 2008).

import piexif

def setImageDateTakenAttribute(filename, date_taken):
exif_dict = piexif.load(filename)
exif_dict['Exif'] = { 
    piexif.ExifIFD.DateTimeOriginal: datetime.datetime(*date_taken[:6]).strftime("%Y:%m:%d %H:%M:%S") 
} 
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, filename)

This works fine in Python 3 for jpeg files, but when I try and use the same code to edit my tif file, I get the following error:

    setImageDateTakenAttribute(full_path, folder_date)
  File "image-and-movie-bulk-creation-dates.py", line 78, in setImageDateTakenAttribute
    piexif.insert(exif_bytes, filename)
  File "C:\Python37\lib\site-packages\piexif\_insert.py", line 39, in insert
    raise InvalidImageDataError
piexif._exceptions.InvalidImageDataError

I have thus far been looking for for other packages that might support my file with no avail.

Does anyone know how the edit can be acomlished in Python without deleting existing exif info, and without changing the image format?

To reproduce or obtain a sample of the failing tif files, clone my project (link bellow).

Details:

After scanning thousands of pictures to tif files I would like to specify the EXIF value for "Date Taken". I am writing a Python script to do this in Windows (BitBucket), that will also to edit "Date Created" and "Date Modified" from a predefined folder naming convention starting with YYYY-MM-DD *. The last two tasks are working for tif files as well as jpeg, but EXIF does not work for tif.

Update:

Running exif tool, I get output without the creation date node, but after setting a date in Windows using file properties, the fields "Create Date" and "Date/Time Original" Appears. Also a raw text printout of XMP meta values gives an added node called xmp:createdate after setting creation date in Windows. Still I cannot figure ot how to create these fields in the file for the first time.



from Add "Date Taken" Exif/XMP information to TIF file using Python

No comments:

Post a Comment