pythonwarningsiptc

Python: Disable iptcinfo warning


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") 

Solution

  • 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