写入exif元数据时,XPComment和XPKeywords不出现

问题描述

在写入exif元数据时,xpcomment和XPKeywords不出现。

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?

解决方法

它需要以utf-16格式编码。所需的编码可能会因计算机而异。

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