Search code examples
pythonpython-3.ximagepython-imaging-libraryjpeg

EXIF: error when trying to retrieve comment from JPEG


using the command line exiftool I added a comment to my JPEG image and now when trying to get the data in python to display it I get an error.

The way I put the comment in the picture was with the following command:

exiftool -Comment=1234 j1.2.jpg

Below is the code of me trying to get the information out the JPEG including the comments but I am having trouble with the _getexif() line as it says there is no attribute _getexif().

from PIL import Image
from PIL.ExifTags import TAGS

image = 'j1.2.jpg'
exifdata = image._getexif()

for k,v in exifdata.items():
    print(str(TAGS.get(k)) + '\t' + str(v))

I am using python 3.9 for reference


Solution

  • Create a file with ImageMagick and insert a comment with exiftool:

    magick -size 640x480 gradient:red-blue image.jpg
    exiftool -Comment="Freddy Frog" image.jpg
    

    Extract comment with PIL:

    #!/usr/bin/env python3
    
    from PIL import Image
    
    im = Image.open('image.jpg')
    comment = im.info['comment']
    print(comment)
    

    Sample Run

    ./PILJPEGComment.py
    b'Freddy Frog'