Home > database >  EXIF: error when trying to retrieve comment from JPEG
EXIF: error when trying to retrieve comment from JPEG

Time:04-18

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

CodePudding user response:

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'

CodePudding user response:

Looking at the jpg-block-structure, there is a tag to embed a comment in the file. Use FFFE to start the comment block and then FF9D to end the file. This should be retained through all media.

  • Related