Python: 禁用 iptcinfo 警告
Python: Disable iptcinfo warning
我正在使用 iptcinfo Python 模块从图片中获取元数据,但它向我抛出了很多此类(无用的)警告:
('WARNING: problems with charset recognition', "'\x1b'")
这是什么意思,我怎样才能删除这些警告(或防止它们发生),因为它们似乎对我的代码不重要?
我的代码很简单:
import iptcinfo
iptc = iptcinfo.IPTCInfo("DSC05647.jpg")
This line in the code 似乎正在生成警告:
LOG.warn('problems with charset recognition %s', repr(temp))
您看到此消息是因为 Python 的日志记录模块的默认日志记录级别是 "warning"。
在您的代码中,您可以将库的记录器 logging level 修改得更高,这样您就不会看到警告:
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应该与Python 2.
完美配合
另一个解决方案是修改原始源代码:
导致警告的原始代码
('WARNING: problems with charset recognition', "'\x1b'")
在 iptcinfo.py 文件的第 971 行。
LOG.warn('problems with charset recognition %s', repr(temp))
您可以 fork 原始 github 存储库并简单地注释掉它
#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
问题是您使用的模块做了一些您不希望模块做的事情。
print (
'WARNING: problems with charset recognition',
repr(temp))
好吧,有些东西不能像那样被禁用。但是他们是关于如何实现相同目标的很好的 SO 线程。
Silence the stdout of a function in Python without trashing sys.stdout and restoring each function call
Suppress calls to print (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)
而且效果很好
我正在使用 iptcinfo Python 模块从图片中获取元数据,但它向我抛出了很多此类(无用的)警告:
('WARNING: problems with charset recognition', "'\x1b'")
这是什么意思,我怎样才能删除这些警告(或防止它们发生),因为它们似乎对我的代码不重要?
我的代码很简单:
import iptcinfo
iptc = iptcinfo.IPTCInfo("DSC05647.jpg")
This line in the code 似乎正在生成警告:
LOG.warn('problems with charset recognition %s', repr(temp))
您看到此消息是因为 Python 的日志记录模块的默认日志记录级别是 "warning"。
在您的代码中,您可以将库的记录器 logging level 修改得更高,这样您就不会看到警告:
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应该与Python 2.
完美配合另一个解决方案是修改原始源代码:
导致警告的原始代码
('WARNING: problems with charset recognition', "'\x1b'")
在 iptcinfo.py 文件的第 971 行。
LOG.warn('problems with charset recognition %s', repr(temp))
您可以 fork 原始 github 存储库并简单地注释掉它
#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
问题是您使用的模块做了一些您不希望模块做的事情。
print (
'WARNING: problems with charset recognition',
repr(temp))
好吧,有些东西不能像那样被禁用。但是他们是关于如何实现相同目标的很好的 SO 线程。
Silence the stdout of a function in Python without trashing sys.stdout and restoring each function call
Suppress calls to print (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)
而且效果很好