hMatoba / Piexif

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

Fix for dumping exif data loaded from images that contain a Gps Versi… #78

Open wynnw opened 5 years ago

wynnw commented 5 years ago

…onID tag

This crash was found when loading exif data from a picture shot with a Canon PowerShot SX50 HS (according to the exif data), and it's Gps/VersionID value was 2.3.0.0 but at run time was loaded as '\x02\x03\x00\x00'.

This probably needs an equivalent fix for Python 3.

I haven't added a test as I'm not sure if this is conceptually the right fix, as it could be a problem with all sorts of fields. I see you haven't addressed #67, so maybe this isn't something you want the library to deal with? Seems like there's way too many interpretations of the exif spec so I understand this is a tough problem.

My use case for using this library was to auto-rotate images using Pillow based on their exif orientiation value, update the exif data's orientation flag, and then save the updated image without losing it's exif data. Right now I'll have to throw away the exif data if there's a problem creating the new exif bytes - it would be nice if it was more reliable to load/save the exif.

What are you thoughts?

coveralls commented 5 years ago

Coverage Status

Coverage decreased (-0.06%) to 94.832% when pulling 9acce90340d3ae2082c10abd8b5a4d404ab53a43 on wynnw:fix/exif-dump-gps-version into 48ced13646e8e85cc0bf30be5874d644c28ccdd4 on hMatoba:master.

Avi1git commented 5 years ago

Hi, I am trying to write coordinates of a different system and save the modified exif to a new image using python 3.6.

exif_dict = piexif.load(img.info['exif'])

breite = exif_dict['GPS'][piexif.GPSIFD.GPSLatitude]
lange = exif_dict['GPS'][piexif.GPSIFD.GPSLongitude]

breite = breite[0][0] / breite[0][1] + breite[1][0] / (breite[1][1] * 60) + breite[2][0] / (breite[2][1] * 3600)
lange = lange[0][0] / lange[0][1] + lange[1][0] / (lange[1][1] * 60) + lange[2][0] / (lange[2][1] * 3600)
x, y = pyproj.transform(wgs84, gk3, lange, breite)

exif_dict['GPS'][piexif.GPSIFD.GPSLatitude] = [ ( (int)(round(y,6) * 1000000), 1000000 ), (0, 1), (0, 1) ]
exif_bytes = piexif.dump(exif_dict)
img.save(os.path.join(outPath,fn), "jpeg", exif=exif_bytes)

I am getting struct.error: argument out of range in dump. The GPSInfo tag looks like: {0: b'\x02\x03\x00\x00', 1: 'N', 2: ((48, 1), (48, 1), (3449322402, 70000000)), 3: 'E', 4: ((9, 1), (57, 1), (1136812930, 70000000)), 5: b'\x00', 6: (3659, 10)}

What am I doing wrong?