Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set time metadata according to timezone the picture was taken in #15

Open
maithyy opened this issue Nov 5, 2024 · 2 comments
Open

Set time metadata according to timezone the picture was taken in #15

maithyy opened this issue Nov 5, 2024 · 2 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@maithyy
Copy link

maithyy commented Nov 5, 2024

hi sorry! not sure what the etiquette is for contributing but when i ran the script and downloaded the images, i noticed that the time metadata was set to UTC by default. i made some changes that set it to the right timezone according to the location where the picture was taken and could contribute my code if you'd like

i added this to the update_exif function to fix it

    if location and 'latitude' in location and 'longitude' in location:
            logging.info(f"Found location: {location}")
            gps_ifd = {
                piexif.GPSIFD.GPSLatitudeRef: 'N' if location['latitude'] >= 0 else 'S',
                piexif.GPSIFD.GPSLatitude: _convert_to_degrees(abs(location['latitude'])),
                piexif.GPSIFD.GPSLongitudeRef: 'E' if location['longitude'] >= 0 else 'W',
                piexif.GPSIFD.GPSLongitude: _convert_to_degrees(abs(location['longitude'])),
            }
            exif_dict['GPS'] = gps_ifd
            logging.info(f"Added GPS location: {gps_ifd}")
    
            longitude = location['longitude']
            offset_hours = longitude / 15.0  # 1 hour per 15 degrees
            offset_timedelta = timedelta(hours=round(offset_hours))

            datetime_local = datetime_original + offset_timedelta
            datetime_str = datetime_local.strftime("%Y:%m:%d %H:%M:%S")
            exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = datetime_str
            logging.info(f"Adjusted capture time to local timezone: {datetime_str}")
    else:
            datetime_str = datetime_original.strftime("%Y:%m:%d %H:%M:%S")
            exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = datetime_str
            logging.error(f"Could not determine timezone. Set capture time to UTC: {datetime_str}")`
@hatobi
Copy link
Owner

hatobi commented Nov 5, 2024

Hey @maithyy, thanks for raising this issue and your contribution, adding the correct timezone is something I completely overlooked. There's not really an etiquette for contributing to this repo, but generally speaking you can always go ahead and open a pull request that I'll check and merge if everything looks good and works for me as well.

Looking at your code, I noticed that you're calculating timezones by an offset depending on the longitude, which is a perfectly fine approximation but may not always be completely accurate as timezones are also influenced by political boundaries. I'm therefore not completely sure how to proceed and can think of two ways:

  • use libraries timezonefinder and pytz to match gps locations to their corresponding time zone
  • ask user, when running the script, if they want to enter a timezone offset and then apply this offset to all photos.

I'm currently leaning towards option one, as it's more flexible when photos have been taken in different locations but I won't have time to come around and implement this for some time. So if it's something, you'd be up to, feel free to fork this repository, implement the timezone matching and create a pull request! :)

@hatobi hatobi added bug Something isn't working enhancement New feature or request labels Nov 5, 2024
@TomSchrier
Copy link

If you use location to determine a user's timezone, be aware that users with "Precise Location" off (see https://support.apple.com/en-us/102647) might see incorrect timezones. Users who don't share location at all should still be able to manually enter their correct timezone.

I just realized all my photos had incorrect GPS data due to not sharing my specific location with the app, so I had to remove the location data. I used a tool called A Better Finder Attributes 7 (see https://www.publicspace.net/ABetterFinderAttributes/index.html) to adjust timestamps based on filenames and include timezone offsets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants