在张量流中加载 tiff 图像时抑制警告似乎不起作用?

问题描述 投票:0回答:2

使用 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')

如何抑制此警告,或者解决产生警告的问题?

python tensorflow warnings tensorflow-datasets suppress-warnings
2个回答
1
投票

如果你想抑制所有警告那么你可以使用

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

0
投票

您可以尝试更改数据的CRS(坐标参考系)。 以 EPSG 为例。

import rasterio
rasterio.env.GTIFF_SRS_SOURCE = 'EPSG'
from pyproj import CRS

#Change CRS of your data
with rasterio.open("your_image.tif", "r+") as dataset:
   # Create new CRS with EPSG encoding
   new_crs = rasterio.crs.CRS.from_epsg(4326)  
   # Update CRS of your data
   dataset.crs = new_crs
   # Read and write back each band to update the metadata in place
   for i in range(1, dataset.count + 1):
      band_data = dataset.read(i)
      dataset.write(band_data, i) 
© www.soinside.com 2019 - 2024. All rights reserved.