Search code examples
pythonpython-imaging-libraryjpegexif

XPComment and XPKeywords does not appear when writing exif metadata


XPComment and XPKeywords does not appear when writing exif metadata.

from PIL import Image
filepath = "yourFilepath.jpg"
image = Image.open(filepath)

XPComment = 0x9C9C
XPKeywords = 0x9C9E
exifdata = image.getexif()
exifdata[XPComment] = "new comment"
exifdata[XPKeywords] = "new keyword;"

image.save(filepath, exif=exifdata)
# ???? where's my exif data yo?

Solution

  • It needs to be encoded in utf-16 format. The necessary encoding may vary depending on your computer.

    from PIL import Image
    filepath = "yourFilepath.jpg"
    image = Image.open(filepath)
    
    XPComment = 0x9C9C
    XPKeywords = 0x9C9E
    exifdata = image.getexif()
    exifdata[XPComment] = "new comment".encode("utf16")
    exifdata[XPKeywords] = "new keyword;".encode("utf16")
    
    image.save(filepath, exif=exifdata)
    # it appears! :D