Why do I get None when writing to Exif data

67 views Asked by At

I'm working on a Django project where the user uploads an image and then they are able to edit the EXIF data and download the edited data written tot the image.

When writing the data back to the image I am getting TypeError: argument of type 'NoneType' is not iterable

It's been quite complicated to work on, but I'll try and get to the point and relevant code. I had to turn the data received from the form into a dictionary containing keys() corresponding to ExifTAG codes.

I'm now following this excellent guide https://blog.matthewgove.com/2022/05/13/how-to-bulk-edit-your-photos-exif-data-with-10-lines-of-python/ by Matthew Gove to write the data back to the image.

I can successfully create the PILLOW_TAGS list and VALUES (the print to console), and i'm filtering any None k/v so pretty stuck on why i'm getting TypeError: argument of type 'NoneType' is not iterable

Any help appreciated.

EDIT - if I 'hardcode' the PILLOW_TAGS and VALUES lists I do not get Nonetype error and the data is written. For example this does not give Nonetype error.

             PILLOW_TAGS = [315, 33432,]
             VALUES = ['foo', 'bar']

Full relevant code

            PILLOW_TAGS = list(filter(None, image_exif_data_with_codes.keys()))
            print("KEYS",PILLOW_TAGS)
            
            VALUES = list(filter(None, image_exif_data_with_codes.values()))
            print("values",VALUES)
            
            pillow_img = PillowImage.open(image.image.path)
            img_exif = pillow_img.getexif()
           
            for tag, value in zip(PILLOW_TAGS, VALUES):
                img_exif[tag] = value
            
            for tag in PILLOW_TAGS:
                try:
                    english_tag = ExifTags.TAGS[tag]
                    value = img_exif[tag]
                except:
                    continue
                print("{}: {}".format(english_tag, value))
           
            pillow_img.save(image.image.path, exif=img_exif)
            image.save()            
                

Traceback

  File "C:\....\.venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\...\.venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\...\core\views.py", line 128, in test_view
    pillow_img.save(image.image.path, exif=img_exif)
  File "C:\...\.venv\lib\site-packages\PIL\Image.py", line 2439, in save
    save_handler(self, fp, filename)
  File "C:\...\.venv\lib\site-packages\PIL\JpegImagePlugin.py", line 777, in _save
    exif = exif.tobytes()
  File "C:\..\.venv\lib\site-packages\PIL\Image.py", line 3792, in tobytes
    and ExifTags.IFD.Interop in value
TypeError: argument of type 'NoneType' is not iterable```




          
0

There are 0 answers