Python:禁用iptcinfo警告

Python:禁用iptcinfo警告,python,warnings,iptc,Python,Warnings,Iptc,我正在使用iptcinfo Python模块从图片中获取元数据,但它向我抛出了许多此类(无用的)警告: ('WARNING:problems with charset recognition',“'\x1b'”) 这意味着什么?我如何删除这些警告(或防止它们发生),因为它们对我的代码似乎并不重要 我的代码很简单: import iptcinfo iptc = iptcinfo.IPTCInfo("DSC05647.jpg") 似乎正在生成警告: LOG.warn('problems with

我正在使用iptcinfo Python模块从图片中获取元数据,但它向我抛出了许多此类(无用的)警告:

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

这意味着什么?我如何删除这些警告(或防止它们发生),因为它们对我的代码似乎并不重要

我的代码很简单:

import iptcinfo
iptc = iptcinfo.IPTCInfo("DSC05647.jpg") 
似乎正在生成警告:

LOG.warn('problems with charset recognition %s', repr(temp))
您看到此消息是因为Python日志模块的默认日志记录级别为“警告”

在代码中,您可以将库的记录器修改为更高,这样就不会看到警告:

import logging
iptcinfo_logger = logging.getLogger('iptcinfo')
iptcinfo_logger.setLevel(logging.ERROR)
编辑:对于疑难解答,这里有一个片段可以查看每个记录器的级别:

for logger_name in logging.Logger.manager.loggerDict:
    logger_level = logging.getLogger(logger_name).level
    print logger_name, logging.getLevelName(logger_level)

首先,我认为iptcinfo应该与Python2完美结合

另一个解决方案是修改原始源代码:

负责警告的原始代码

('WARNING: problems with charset recognition', "'\x1b'")
位于iptcinfo.py文件的第971行

LOG.warn('problems with charset recognition %s', repr(temp))
您可以将原始的github回购协议分叉,然后简单地将其注释掉

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


问题是您使用的模块做了一些您不希望模块做的事情

print (
       'WARNING: problems with charset recognition',
      repr(temp))
有些东西不能像那样被禁用。但是他们的线程都很好,所以如何实现同样的效果

所以把两者结合起来

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)
而且效果很好


我刚刚尝试了该代码,但仍收到警告导入
iptcinfo
后,在运行函数之前,您是否执行了该操作?查看Python以更好地调试哪些记录器处于活动状态以及它们处于何种日志级别。我尝试了该代码以查看级别。iptcinfo确实设置为错误,但我仍然收到警告消息@Sulli抱歉,我不确定你在做什么。请显示一个代码片段。@mouche我刚刚添加了一个代码片段。在“导入”后插入代码不会改变任何东西您使用的是什么版本的iptcinfo?如果您不知道,请尝试打印
iptcinfo.\uuuu version\uuuuu
以查看。这就是我的答案所指的版本。请参阅我的编辑以调试记录器和级别。我尚未尝试,但我认为问题可能是图像中与图像元数据相关的信息是unicode,这就是库出现问题的原因。我希望您尝试Python3的原因是,如果这个问题在同一个版本中得到修复,它会给出一个想法。Python3默认使用unicode,因此如果我们知道它在Python3中工作,那么我们可以肯定Python2中存在unicode/str转换问题。因此,如果您可以尝试使用Python3,这将有助于找到一些解释。因此需要注意的是,github上的代码与通过pip安装的代码有一些不同。该包可能不是最近推出的,也不是使用最新代码推出的
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)