如何在JPG图像的IPTC数据中附加关键字?

时间:2019-01-17 06:09:01

标签: python iptc

我试图将关键字添加到JPG文件中的IPTC数据中,但失败了。我可以使用iptcinfo3库读取关键字,并且可以将关键字附加到当前关键字列表中,但是尝试尽快将这些关键字写回JPG文件时失败。该错误消息对我来说还不太清楚,并且可能实际上引用了new关键字的附加内容(尽管一条打印语句似乎表明它已采用)。

我尝试了三个不同的元数据库(似乎不是一个标准),这是我对它们中的任何一个的最深入的了解(甚至无法安装一个,而无法再获得一个)跑步)。这似乎很基础,但是我无法弄清楚,也无法适应我在网上看到的其他几个代码示例,包括iptcinfo3的示例代码片段。

当前错误消息是:

| => pipenv run python editMetadata.py
WARNING: problems with charset recognition (b'\x1b')
[b'Gus']
[b'Gus', b'frog']
Traceback (most recent call last):
  File "editMetadata.py", line 22, in <module>
    info.save_as('Gus2.jpg')
  File "/Users/Scott/.local/share/virtualenvs/editPhotoMetadata-tx0JAOmI/lib/python3.7/site-packages/iptcinfo3.py", line 635, in save_as
jpeg_parts = jpeg_collect_file_parts(fh)
  File "/Users/Scott/.local/share/virtualenvs/editPhotoMetadata-tx0JAOmI/lib/python3.7/site-packages/iptcinfo3.py", line 324, in jpeg_collect_file_parts
adobeParts = collect_adobe_parts(partdata)
  File "/Users/Scott/.local/share/virtualenvs/editPhotoMetadata-tx0JAOmI/lib/python3.7/site-packages/iptcinfo3.py", line 433, in collect_adobe_parts
out = [''.join(out)]
TypeError: sequence item 0: expected str instance, bytes found

代码:

from iptcinfo3 import IPTCInfo
import os


# Create new info object
info = IPTCInfo('Gus.jpg')

# Print list of keywords
print(info['keywords'])

# Append the keyword I want to add
info['keywords'].append(b'frog')

# Print to test keyword has been added
print(info['keywords'])

# Save new info to file
info.save()
info.save_as('Gus2.jpg')

2 个答案:

答案 0 :(得分:0)

而不是附加使用等于“ =”

from iptcinfo3 import IPTCInfo

info = IPTCInfo('Gus.jpg')
print(info['keywords'])

# add keyword
info['keywords'] = ['new keyword']

info.save()
info.save_as('Gus_2.jpg')

答案 1 :(得分:0)

我有同样的错误。保存取决于文件似乎是个问题。

from iptcinfo3 import IPTCInfo
info = IPTCInfo('image.jpg', force=True)
info.save()

这给了我同样的错误。

WARNING: problems with charset recognition (b'\x1b')
WARNING: problems with charset recognition (b'\x1b')
Traceback (most recent call last):
  File "./searchimages.py", line 123, in <module>
    main(sys.argv[1:])
  File "./searchimages.py", line 119, in main
    find_photos(str(sys.argv[1]))
  File "./searchimages.py", line 46, in find_photos
    write_keywords(image, current_keywords, new_keywords)
  File "./searchimages.py", line 109, in write_keywords
    info.save_as('out.jpg')
  File "/usr/local/lib/python3.7/site-packages/iptcinfo3.py", line 635, in save_as
    jpeg_parts = jpeg_collect_file_parts(fh)
  File "/usr/local/lib/python3.7/site-packages/iptcinfo3.py", line 324, in jpeg_collect_file_parts
    adobeParts = collect_adobe_parts(partdata)
  File "/usr/local/lib/python3.7/site-packages/iptcinfo3.py", line 433, in collect_adobe_parts
    out = [''.join(out)]
TypeError: sequence item 0: expected str instance, bytes found