LeoHsiao1 / pyexiv2

Read and write image metadata, including EXIF, IPTC, XMP, ICC Profile.
GNU General Public License v3.0
196 stars 39 forks source link

IPTC中文乱码 #107

Closed chu-shen closed 1 year ago

chu-shen commented 1 year ago

问题

以下代码写入IPTC元数据后,在linux上使用exiftool查看时乱码,但windows属性里显示正常

hashtags=['マリンのお宝']
img = pyexiv2.Image(filename)
iptc = {
                'Iptc.Application2.Keywords': hashtags
            }
# 乱码:マリンのお宝 -> ã, マリãƒ, šå
 img.modify_iptc(iptc)

是不是没有对中文字符单独处理?👉编码时需要对中英文字符分开处理

另外附上之前直接使用exiftool正确写入时的命令:exiftool -q -overwrite_original -iptc:all -codedcharacterset=utf8 -charset iptc=UTF8 -keywords="${hashtags}" "$file"

https://exiftool.org/faq.html#Q10

还请作者看到后修复一下,感谢

LeoHsiao1 commented 1 year ago

你试试指定别的编码格式?

img.modify_iptc(data, encoding='utf-8')
img.modify_iptc(data, encoding='gbk')

另外还需要考虑,Linux上用exiftool命令能显示日文吗?

chu-shen commented 1 year ago

你试试指定别的编码格式?

img.modify_iptc(data, encoding='utf-8')
img.modify_iptc(data, encoding='gbk')

另外还需要考虑,Linux上用exiftool命令能显示日文吗?

这三种都不行,image和modify_iptc都试过了: img = pyexiv2.Image(path, encoding='utf-8') img = pyexiv2.Image(path, encoding='GBK') img = pyexiv2.Image(path, encoding='ISO-8859-1')

日文是能正常显示的

chu-shen commented 1 year ago

红框中,exif ImageDescription编码正常,iptc Keywords则乱码 image

LeoHsiao1 commented 1 year ago

图片文件里写入的 iptc 数据都是一样的,在 windows、linux 上查看的效果不一样,只能说明两种软件的读取逻辑不同。 我尝试用 exiftool 写入了这个日文 keywords :

$ hashtags='マリンのお宝'
$ exiftool -q -overwrite_original -iptc:all -codedcharacterset=utf8 -charset iptc=UTF8 -keywords="${hashtags}" 1.jpg

然后用 pyexiv2 读取图片:

>>> img.read_iptc()
{'Iptc.Envelope.CharacterSet': '\x1b%G', 'Iptc.Envelope.ModelVersion': '4', 'Iptc.Application2.Keywords': ['マリンのお宝'], 'Iptc.Application2.RecordVersion': '4'}

所以这说明 exiftool 会额外写入 'Iptc.Envelope.CharacterSet' 标签来控制编码格式。 因此你需要写入两种标签:

>>> img.modify_iptc({'Iptc.Envelope.CharacterSet': '\x1b%G', 'Iptc.Application2.Keywords': ['マリンのお宝']})
>>> img.read_iptc()
{'Iptc.Envelope.CharacterSet': '\x1b%G', 'Iptc.Application2.Keywords': ['マリンのお宝']}
chu-shen commented 1 year ago

已解决,感谢

           iptc = {
                'Iptc.Envelope.CharacterSet': '\x1b%G',
                'Iptc.Application2.Keywords': self.hashtags
            }