LeoHsiao1 / pyexiv2

Read and write image metadata, including EXIF, IPTC, XMP, ICC Profile.
GNU General Public License v3.0
201 stars 39 forks source link

Writing arrays into exif segment is not working #12

Closed xavieldar closed 4 years ago

xavieldar commented 4 years ago

Some Exif tags store a list of values, such as GPS latitude and longitude, which take a list of three rationals to store the angle in a "degrees minutes seconds" format.

For example, one would expect to be able to write something in the form

image.modify_exif({
    'Exif.GPSInfo.GPSLongitude': ['30/1', '50/1', '12345/1000'],
    'Exif.GPSInfo.GPSLatitude': ['30/1', '50/1', '12345/1000'],
})

and read back successfully the values.

Looking at the api C++ code, arrays seems to be supported in the XMP segments. I believe that porting the same behaviour to the exif part should be straightforward.

Thanks for the good work!

LeoHsiao1 commented 4 years ago

Hi! There are many types of metadata, so I didn't try to see if each tag could be modified.

I tried to read the tag you mentioned and found it was like this:

>>> import pyexiv2
>>> img = pyexiv2.Image(r'C:\Users\Leo\Desktop\IMG_20200105_115821.jpg')
>>> img.read_exif()['Exif.GPSInfo.GPSLongitude']
'114/1 2/1 426696/10000'

So I modified it in the same format. It works:

>>> img.modify_exif({'Exif.GPSInfo.GPSLongitude': '200/1 2/1 400000/10000'})
>>> img.read_exif()['Exif.GPSInfo.GPSLongitude']
'200/1 2/1 400000/10000'

Next, you just need to convert it from string to list:

>>> '200/1 2/1 400000/10000'.split(' ')
['200/1', '2/1', '400000/10000']
>>> ' '.join(['200/1', '2/1', '400000/10000'])
'200/1 2/1 400000/10000'

Does that solve your problem?

xavieldar commented 4 years ago

Hi,

Thanks for the quick answer. Actually, that works, thanks!

I though I tried that, but I was converting Fraction object into string directly with fractions.Fraction.__str__(), which does not print out the denominator when equals to 1, which gave me: '30 50 12345/1000' instead of '30/1 50/1 12345/1000', and would not work… This is fixed on my side and I can properly geotag images.

Thanks.

github-actions[bot] commented 3 years ago

This issue has been automatically closed because there has been no activity for a month.