通过在 tensorflow 中加载 tiff 图像来抑制警告似乎不起作用?

Suppress warnings with loading tiff images in tensorflow doesn't seem to work?

我在使用 tfio.experimental.image.decode_tiff 时不断收到以下警告。我发现它工作正常,但一直发出这些警告,我想抑制这些警告。

TIFFFetchNormalTag: Warning, ASCII value for tag "DateTime" contains null byte in value; value incorrectly truncated during reading due to implementation limitations.

我是这样使用的:

string = tf.io.read_file(filename)
image = tfio.experimental.image.decode_tiff(string) # this line produces warning

如果我尝试使用 warning 来抑制警告,它似乎不起作用?它没有给我一个错误,但它什么也没做。

import warnings
warnings.filterwarnings('ignore', message='ASCII value for tag "DateTime" contains null byte in value; value incorrectly truncated during reading due to implementation limitations')

如何禁止显示此警告,或解决产生警告的问题?

如果你想禁止所有警告,那么你可以使用

  warnings.filterwarnings("ignore")

如果要抑制某些消息,则必须在消息开头使用 message=... 或在开头使用 .*

  warnings.filterwarnings("ignore", message=".*ASCII value for tag")

最小示例:

import warnings

warnings.filterwarnings("ignore", message=".*ASCII value for tag")

# some tests - it should be supressed by `filterwarnings()`
warnings.warn('TIFFFetchNormalTag: Warning, ASCII value for tag "DateTime" contains null byte in value; value incorrectly truncated during reading due to implementation limitations.')

print("Hello World")