ianare / exif-py

Easy to use Python module to extract Exif metadata from digital image files.
BSD 3-Clause "New" or "Revised" License
832 stars 191 forks source link

Serialization of EXIF data for mongodb, JSON etc. #110

Open jtlz2 opened 4 years ago

jtlz2 commented 4 years ago

I need to be able to serialize the tags dictionary into a format ingestible by mongodb e.g. JSON. Right now all the tags are of <type 'instance'> though.

PIL's image._getexif() does this - while also preserving types such as float, int, bool etc.

Are you able to assist?

Excellent package of course - for which many thanks.

maxlaverse commented 4 years ago

Is this a duplicate of https://github.com/ianare/exif-py/issues/59 ?

ianare commented 4 years ago

I had actually started work on this a while ago, the main problem is not breaking backwards compatibility. The code is a bit hairy on this. You could do a split on the first space in the tag dict...

tenuki commented 3 years ago

I had actually started work on this a while ago, the main problem is not breaking backwards compatibility. The code is a bit hairy on this. You could do a split on the first space in the tag dict...

Problem isn't the keys but the values, as they are instances and pymongo doesn't know how to handle them.

In my case I do:

            tags = exifread.process_file(f)
            tags = {k:str(v).strip()  for k, v in tags.items()}

to convert output in a new dictionary. I would have submitted a patch with this if the library returned a class where to add this.

I would make sense to create and return the class hdr and that anyone can call tags() or perhaps a new json() method whenever it be needed.

Also, in that chase, I would also replace k for k.replace(' ', '.') in my code above.

tenuki commented 3 years ago

Of course, as it is commented on #59, it would be better if you can avoid to str(v) and keep value's type in json()'s output..