picamera2
picamera2 copied to clipboard
[HOW-TO] Is there a way to add / update exif metadata
I would like to augment the JPEG image file with additional metadata, specifically GPS location and the time that the photo was taken. I can add these via e.g. exiv2 (Exif.Image.DateTime, Exif.Image.DateTimeOriginal, Exif.GPSInfo.GPSLatitudeRef, Exif.GPSInfo.GPSLatitude, Exif.GPSInfo.GPSLongitudeRef, Exif.GPSInfo.GPSLongitude), however, I cannot see a way to add directly.
I have tried altering the metadata in a pre_callback method but without luck, e.g. :
def add_metadata(request):
metadata = request.get_metadata()
print("In pre_callback ExposureTime: " + str(metadata["ExposureTime"]))
metadata["DateTime"] = "2023:01:29 16:25:00"
metadata["DateTimeOriginal"] = "2023:01:29 16:25:00"
print("In pre_callback DateTime: " + str(metadata["DateTime"]))
print("In pre_callback DataTimeOriginal: " + str(metadata["DateTimeOriginal"]))
Hi, thanks for the question. Indeed, I don't think there's a built-in way to accomplish this at the moment, and as you've discovered, you can't add arbitrary values to the image metadata and have the code understand what to do with them.
We can look into the question, but in the meantime is there a workaround where you write the JPEG to a memory buffer, then use one of Python's EXIF packages to amend it, and then re-save it to file?
Hi, thanks for the quick reply. Yes - currently doing a call out to exiv2. Would be really nice if at least DateTime / DateTimeOriginal was written by picamera2.
Yes, I'll certainly add the dates. For adding custom info, that probably just needs a bit of plumbing. We'll put it on the list!
Brilliant, thank you.
https://github.com/raspberrypi/picamera2/pull/552
I'd like to second this. The problem with patching the EXIF after file generation is that you tend to end up compressing the file twice.
Hi, there was a PR to add some extra functionality in this area (https://github.com/raspberrypi/picamera2/pull/786), though it seems to be a bit stalled at the moment. Although not ideal, it should be possible in the meantime to update just the exif tags using either Python or something like exiftool without re-encoding.
I would like to know if this issue has evolved. I particularly need the GPS metadata. A bit of context, I intend to employ the AI cam to make detections and record the geographic coordinates from where they were obtained.
Some of the methods like picam2.capture_file(...) have an exif_data argument that looks like it should accept a dictionary using piexif fields. Is that what you're looking for?