james-see / iptcinfo3

iptcinfo working for python 3 finally do pip3 install iptcinfo3
51 stars 31 forks source link

TypeError: ord() expected string of length 1, but NoneType found #21

Closed fishcode16 closed 10 months ago

fishcode16 commented 4 years ago

Sample Image file from Meetup.com https://secure.meetupstatic.com/photos/event/5/7/3/9/highres_489862329.jpeg

using python 3.8.0 and latest version of IPTCInfo3 2.1.4 work with all of the images I have so far, except those from meetup.com.

Please advise.

Thanks

script:

f = "highres_489862329.jpeg"
info = IPTCInfo(f, force=True)
info.save()

output:

Marker scan hit start of image data
Traceback (most recent call last):
  File "C:\TEST iptcinfo\iptc.py", line 11, in <module>
    info.save()
  File "C:\Users\random-user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\iptcinfo3.py", line 627, in save
    return self.save_as(self._filename, options)
  File "C:\Users\random-user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\iptcinfo3.py", line 636, in save_as
    jpeg_parts = jpeg_collect_file_parts(fh)
  File "C:\Users\random-user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\iptcinfo3.py", line 265, in jpeg_collect_file_parts
    marker = ord(jpeg_next_marker(fh))
TypeError: ord() expected string of length 2, but NoneType found
petrst commented 3 years ago

Hi, same error here:

  File "annotate.py", line 33, in update_iptc
    info.save()
  File "c:\Users\psturc\Downloads\gallery-dl\venv\lib\site-packages\iptcinfo3.py", line 627, in save
    return self.save_as(self._filename, options)
  File "c:\Users\psturc\Downloads\gallery-dl\venv\lib\site-packages\iptcinfo3.py", line 636, in save_as
    jpeg_parts = jpeg_collect_file_parts(fh)
  File "c:\Users\psturc\Downloads\gallery-dl\venv\lib\site-packages\iptcinfo3.py", line 265, in jpeg_collect_file_parts
    marker = ord(jpeg_next_marker(fh))
TypeError: ord() expected string of length 1, but NoneType found

flickr_12203912545

Image attached.

james-see commented 3 years ago

@petrst I was able to use it fine:

import iptcinfo3
info = iptcinfo3.IPTCInfo("/Users/jc/Downloads/test.jpg")
print(info)

resulting in:

$ python3 iptcinfotest1.py
WARNING: problems with charset recognition (b'\x1b')
charset:        None
data:   {'supplemental category': [], 'keywords': [], 'contact': [], 'nonstandard_230': b'https://flickr.com/e/6dOflm61YZeqanFe9cs4s8VSwPHBhwQ4YERykdKJl5o%3D'}

So my guess would be to try to print the info object before saving it. My guess would be is that you have the path wrong on the import so it is an empty file or doesn't exist at the location it is calling from. Since you are using Force, this would not be apparent.

james-see commented 3 years ago

@petrst please let me know what you find out so I can close out this issue. Thanks

petrst commented 3 years ago

@jamesacampbell - the problem appears only when you call info.save(). try following:

import iptcinfo3
info = iptcinfo3.IPTCInfo('test.jpg')
print(info)
info['headline']=['My headline']
info.save()

I did investigate further. It looks like the attached JPEG doesn't have neither APP0 nor APP1 markers. Is it still valid JFIF file? Guess not. Though the JPEG is downloaded from Flickr 🤔

james-see commented 3 years ago

@jamesacampbell - the problem appears only when you call info.save(). try following:

import iptcinfo3
info = iptcinfo3.IPTCInfo('test.jpg')
print(info)
info['headline']=['My headline']
info.save()

I did investigate further. It looks like the attached JPEG doesn't have neither APP0 nor APP1 markers. Is it still valid JFIF file? Guess not. Though the JPEG is downloaded from Flickr 🤔

Yeah that is weird. @petrst how do you think we should handle this? Should we try to ignore that it doesn't find markers and tries to still write out? Warn the user and move on, or ask the user to convert the file to a png and try again? I am open to suggestions.

achristlex commented 3 years ago

Was this ever solved? I am not sure how to either get around the error / continuing a loop since the file isn't markedly different in any particular way and since I'm tagging thousands of files, individually identifying the errored ones isn't viable.

As noted, it only happens when attempting to info.save().

 File "c:\Users\Alex\Desktop\TimetoCode\IPTC_merge_v1.1.py", line 25, in <module>
    info.save()
  File "C:\Users\Alex\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\iptcinfo3.py", line 627, in save
    return self.save_as(self._filename, options)
  File "C:\Users\Alex\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\iptcinfo3.py", line 636, in save_as
    jpeg_parts = jpeg_collect_file_parts(fh)
  File "C:\Users\Alex\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\iptcinfo3.py", line 265, in jpeg_collect_file_parts
    marker = ord(jpeg_next_marker(fh))
TypeError: ord() expected string of length 1, but NoneType found
ammacdonald3 commented 1 year ago

Until a different fix is implemented, I was able to avoid this issue by using the Pillow module to convert to RGB then JPG again before running the IPTCInfo3 code. In the example below, "full_path" is a variable I'm using representing the file path.

from PIL import Image im = Image.open(full_path) rgb_im = im.convert('RGB') updated_path = full_path + ".jpg" rgb_im.save(full_path)

james-see commented 1 year ago

Thanks for the update on this. I have zero time to update this package. I hope people who use it can do some PR’s to update some of these longstanding limitations / bugs.

james-see commented 10 months ago

@ammacdonald3 I would follow this pre-processing to standardize in a large pipeline anyway. IPTCInfo is expecting something, and you should provide it what it expects. Having IPTCInfo do any more than that or try to figure it out is a topic for debate, but not here at this time.