4

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

('警告:字符集识别问题', "'\x1b'")

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

我的代码很简单:

import iptcinfo
iptc = iptcinfo.IPTCInfo("DSC05647.jpg") 
4

3 回答 3

2

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

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

好吧,不能像那样被禁用。但是,它们是关于如何实现相同目标的良好 SO 线程。

在 Python 中静默函数的标准输出,而不破坏 sys.stdout 并恢复每个函数调用

禁止调用打印(python)

所以将两者结合起来

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)

而且效果很好

无打印

于 2018-05-29T20:13:27.160 回答
2

代码中的这一行似乎正在生成警告:

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)
于 2018-05-18T09:17:47.027 回答
1

首先,我认为 iptcinfo 应该与 Python 2 完美配合。

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

负责警告的原始代码

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

位于 iptcinfo.py 文件的第 971 行。

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

你可以 fork 原始的 github repo 并简单地把它注释掉

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

然后

#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
于 2018-05-28T03:33:37.020 回答