4

I am using the iptcinfo Python module to get metadata from a picture but it is throwing at me a lot of (useless) warnings of this type:

('WARNING: problems with charset recognition', "'\x1b'")

What does it mean and how can I remove these warnings (or prevent them from happening) as they don't seem to be important for my code in any way?

My code is simply:

import iptcinfo
iptc = iptcinfo.IPTCInfo("DSC05647.jpg") 
Sulli
  • 763
  • 1
  • 11
  • 33
  • Please show a code snippet. – mouche May 18 '18 at 09:53
  • @mouche I just added a code snippet. Inserting your code after "import" does not change anything – Sulli May 18 '18 at 10:04
  • What version of iptcinfo are you using? If you don't know, try printing `iptcinfo.__version__` to see. – mouche May 18 '18 at 10:07
  • @mouche I'm using iptcinfo version 1.9.5-6 – Sulli May 18 '18 at 10:14
  • 1
    That's the version my answer refers to. See my edit for debugging loggers and levels. – mouche May 18 '18 at 10:21
  • @mouche thanks, but it goes beyond my skills to understand this man page. Were you able to reproduce my problem? – Sulli May 18 '18 at 10:36
  • You never mentioned the python version? Can you provide a sample image or does it happen with every image? – Tarun Lalwani May 23 '18 at 18:04
  • @TarunLalwani I'm using python 2.7.12. I tried with a random image from the web and I don't get the warning. So it might be something specific to my images. My images are exported from Adobe Lightroom, the IPTC data is added by Lightroom. Here is one of my images that is bringing the warning: https://ibb.co/n8mQko – Sulli May 24 '18 at 14:18
  • Is try Python 3 an option? If so I would suggest to try that too – Tarun Lalwani May 24 '18 at 14:21
  • @TarunLalwani unfortunately it's not an option for me. Can you reproduce the problem? – Sulli May 24 '18 at 19:13
  • 1
    I haven't tried yet, but I think the issue may be image metadata related info in your image is unicode and that is why the library is having an issue. The reason I wanted you to try Python 3 was, it would give an idea if issue is fixed in the same. Python 3 by default uses unicode, so if we knew it works in Python 3, we are sure it is a unicode/str conversion issue in case of Python 2. So if you could try in Python 3, that would help find some explanation faster – Tarun Lalwani May 24 '18 at 19:18

3 Answers3

3

This line in the code appears to be generating the warning:

LOG.warn('problems with charset recognition %s', repr(temp))

You're seeing this message because the default logging level for Python's logging module is "warning".

In your code, you could modify the library's logger's logging level to be higher so you don't see warnings:

import logging
iptcinfo_logger = logging.getLogger('iptcinfo')
iptcinfo_logger.setLevel(logging.ERROR)

Edit: For troubleshooting, here's a snippet to see the levels for every logger:

for logger_name in logging.Logger.manager.loggerDict:
    logger_level = logging.getLogger(logger_name).level
    print logger_name, logging.getLevelName(logger_level)
mouche
  • 1,785
  • 1
  • 16
  • 22
  • I just tried that code and I'm still getting the warnings – Sulli May 18 '18 at 09:40
  • Did you do it after importing `iptcinfo` and before running the function? – mouche May 18 '18 at 09:53
  • Look into Python's [logging module](https://docs.python.org/2/library/logging.html) to better debug what loggers are active and what logging level they're at. – mouche May 18 '18 at 09:58
  • I tried the code to see the levels. iptcinfo is indeed set to ERROR but I still get the warning messages! – Sulli May 20 '18 at 07:53
  • @Sulli Sorry, I'm not sure what's going on for you. – mouche May 21 '18 at 14:16
  • Great answer, and it worked for me, on IPTCInfo3-2.1.4. – nealmcb Apr 02 '21 at 02:48
  • See also my issue documenting how the module seems to be parsing the charset wrong in the first place: [WARNING: problems with charset recognition \(b'\\x1b'\) from parsing Iptc\.Envelope\.CharacterSet incorrectly · Issue \#32 · jamesacampbell/iptcinfo3](https://github.com/jamesacampbell/iptcinfo3/issues/32) – nealmcb Apr 02 '21 at 03:50
2

The issue is that the module you use does something you don't expect modules to do.

print (
       'WARNING: problems with charset recognition',
      repr(temp))

Well something that can't be just disabled like that. But then their are good SO threads on how to achieve the same.

Silence the stdout of a function in Python without trashing sys.stdout and restoring each function call

Suppress calls to print (python)

So combining both of them

import iptcinfo

origianl_IPTCInfo = iptcinfo.IPTCInfo

def patch_IPTCInfo(*args, **kwargs):
    import os, sys

    class HiddenPrints:
        def __enter__(self):
            self._original_stdout = sys.stdout
            sys.stdout = open('/dev/null', 'w')

        def __exit__(self, exc_type, exc_val, exc_tb):
            sys.stdout = self._original_stdout

    with HiddenPrints():
        return origianl_IPTCInfo(*args, **kwargs)

iptcinfo.IPTCInfo = patch_IPTCInfo

iptc = iptcinfo.IPTCInfo("/Users/tarunlalwani/Downloads/image.jpg")
print(iptc)

and it just works great

No Print

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • Also one thing to note, the code on github and and the one that gets installed through pip has some difference. Probably the package is not be pushed recently or with latest code – Tarun Lalwani May 29 '18 at 20:26
1

First of all, I think iptcinfo should work perfectly with Python 2.

Another solution would be modifying the original source code:

The original code responsible for the warning

('WARNING: problems with charset recognition', "'\x1b'")

is at line 971 in the iptcinfo.py file.

LOG.warn('problems with charset recognition %s', repr(temp))

You may fork the original github repo and simply comment it out

#LOG.warn('problems with charset recognition %s', repr(temp))

Then

#Uninstall the original installation
pip uninstall iptcinfo
#Do pip install from your own fork. e.g.:
pip install git+git://github.com/Sulli/iptcinfo.git
mcgag
  • 325
  • 2
  • 14