3

I'm trying to add keywords to the IPTC data in a JPG file and failing miserably. I'm able to read in the keywords using the iptcinfo3 library and, seemingly, append the keyword to the list of current keywords but I'm failing when trying to write those keywords back to the JPG file, if not sooner. The error message is a bit unclear to me and may actually reference the appending of the new keyword (although a print statement seems to indicate it took).

I've tried three different metadata libraries (there doesn't seem to be one standard) and this is the furthest I've gotten with any of them (failing to even install one and not being able to get a second one to run). This seems so basic but I can't figure it out and haven't been able to adapt the few other code examples I've seen online to work, including iptcinfo3's example code fragment.

The current Error message is:

| => 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

Code:

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')
james-see
  • 12,210
  • 6
  • 40
  • 47
Scott
  • 31
  • 1
  • 4
  • This has been fixed as of 2.1.4 release (I maintain this pypi repo). Please upgrade iptcinfo3 to the latest version. If it is still not working for you submit an issue in github. – james-see Jul 06 '19 at 04:37

2 Answers2

2

Instead of appending use equal "="

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')
0

I have the same error. It seems to be an issue with the save depending on the file.

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

Which gives me the same error.

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
John Croucher
  • 169
  • 2
  • 9
  • 1
    This has been fixed, it was a bytes issue when we ported it from Python 2 to Python 3. It is fixed now in the latest version 2.1.4. Please upgrade via `pip3 install iptcinfo3 -U` and try it again. Should be working great now. – james-see Jul 06 '19 at 04:38