[Answered ]-Getting "TypeError: Object of type IFDRational is not JSON serializable" while trying to json.dumps EXIF info

1👍

When checking for type on each of the items you can see that some items in the dict are actually of type PIL.TiffImagePlugin.IFDRational.

for (k, v) in image_getexif.items():
    print(k, type(k))
    print(v, type(v))

This outputs:

296 <class 'int'>
2 <class 'int'>
34665 <class 'int'>
204 <class 'int'>
271 <class 'int'>
Apple <class 'str'>
272 <class 'int'>
iPhone 13 <class 'str'>
305 <class 'int'>
15.3.1 <class 'str'>
274 <class 'int'>
1 <class 'int'>
306 <class 'int'>
2022:03:04 17:35:15 <class 'str'>
282 <class 'int'>
72.0 <class 'PIL.TiffImagePlugin.IFDRational'>
283 <class 'int'>
72.0 <class 'PIL.TiffImagePlugin.IFDRational'>
316 <class 'int'>
iPhone 13 <class 'str'>
{}
<class 'dict'>
{}

Add these to the exclusions (or cast them to int if required) and you’re good to go.

if k in ExifTags.TAGS and type(v) not in [bytes, TiffImagePlugin.IFDRational]

Import the TiffImagePlugin from PIL:

from PIL import Image, ExifTags, TiffImagePlugin
👤SaeX

Leave a comment