1

I'm trying to write EXIF data to a jpg file using Pillow and Python 3. I don't understand why the following code is failing. Any help would be greatly appreciated.

fileName is a valid full path and file name to a jpg file. I'm accessing the name from a QListWidget that displays photo file names and their images as icons. The overall idea is to iterate through the list of files in the QListWidget and update each file's EXIF data.

    while S < self.lstPhotos.count():
        fileName = str(self.lblPath.text() + "/" + self.lstPhotos.item(S).text())
        im = Image.open(fileName)
        exifdata = im._getexif()
        exifdata[0x010f] = "Canon"
        exifdata[0x0110] = "EOS 630"
        im.save(fileName, exif=exifdata)
        S += 1

The error I receive is:

Traceback (most recent call last):
  File "/home/richard/Dropbox/Documents/Personal/Python/PhotOrg/src/photoOrgMain.py", line 372, in WriteEXIFData
im.save(fileName, exif=exifdata)
File "/home/richard/.local/lib/python2.7/site-packages/PIL/Image.py", line 1698, in save
save_handler(self, fp, filename)
File "/home/richard/.local/lib/python2.7/site-packages/PIL/JpegImagePlugin.py", line 725, in _save
ImageFile._save(im, fp, [("jpeg", (0, 0)+im.size, 0, rawmode)], bufsize)
File "/home/richard/.local/lib/python2.7/site-packages/PIL/ImageFile.py", line 490, in _save
e = Image._getencoder(im.mode, e, a, im.encoderconfig)
File "/home/richard/.local/lib/python2.7/site-packages/PIL/Image.py", line 439, in _getencoder
return encoder(mode, *args + extra)
TypeError: argument 13 must be string or read-only buffer, not dict

I'm guessing that Pillow does not want the EXIF data in dictionary form, but that's the format Pillow itself retrieves from the file.

I can't find documentation online about what format the EXIF data should be in. I assumed it should be in the same format that Pillow generated with _getexif() call.

I am using Python 3. I'm using the latest version of Pillow. Pillow installed itself at .local/lib/python2.7/site-packages/PIL/ and its other functions seem to be working under Python 3.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
trinkner
  • 374
  • 4
  • 15
  • `TypeError: argument 13 must be string or read-only buffer, not dict` - it seems you have to create one string with all data. – furas Nov 16 '16 at 19:10
  • why do you have `PIL` in folder for modules for `Python2.7` if you use `Python 3` ??? – furas Nov 16 '16 at 19:11
  • 1
    BTW: `_` in name `_getexif()` means it is internal function and shouldn't be used by user - so don't expect that `im.save` will use the same format as `_getexif()` returns. – furas Nov 16 '16 at 19:15
  • Thanks for the suggestions. Does anybody know the proper format for the EXIF data in the save call? (Also, PIL installed itself in the 2.7 directory. Can I move it to the Python 3 directory, or would I need to reinstall?) – trinkner Nov 16 '16 at 19:19
  • every Python uses own version of module and keep it in own folder. You should have `pip` (`pip2`) and `pip3` to install modules for different versions. – furas Nov 16 '16 at 19:22
  • Maybe see http://stackoverflow.com/a/33885893/5987 for a possible solution. – Mark Ransom Nov 16 '16 at 19:31

0 Answers0