hMatoba / Piexif

Exif manipulation with pure python script.
MIT License
367 stars 81 forks source link

On some file, the value extracted for the field "DateTimeOriginal" is truncated. #134

Open hsq125 opened 2 years ago

hsq125 commented 2 years ago

For some image, the following code will truncate the DateTimleOriginal.

import piexif
path='20_BIKE_C_4_002.jpg'
if __name__ == '__main__':
    exif_dict = piexif.load(path)
    ts = exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal]
    print(ts)

will produce

b'2021:08:06 16:10:4'

instead of

b'2021:08:06 16:10:41'

The test image can be downloaded here https://issue-report-resources.s3.eu-central-1.amazonaws.com/piexif/20_BIKE_C_4_002.jpg

Important to be noted that the date is correctly extracted by other tools such as exiftool on Linux.

ExifTool Version Number         : 11.88
File Name                       : 20_BIKE_C_4_002.jpg
[...]
Date/Time Original              : 2021:08:06 16:10:41
JFIF Version                    : 1.02
[...]

Octave

Dobatymo commented 1 year ago

@hsq125 I checked this file. According to the spec, DateTimeOriginal is a ascii string which must be \0 terminated and the tag length must include the \0 terminator. In your file the length (19) does not include the \0 terminator thus making it a invalid exif structure. Because of that, piexif cuts of the last character.

Other libraries are smart enough to handle this by reading the full length for the field and simply cutting of \0 bytes if necessary.

I encountered various files like this as well, so maybe piexif could implement this smarter handling as well, even though these files are against the spec.