抑制RasterIO警告。警告1:TIFFReadDirectory。

3
我一直遇到一个错误消息:警告1:TIFFReadDirectory:与SamplesPerPixel不匹配的颜色通道和ExtraSamples总和。将非颜色通道定义为ExtraSamples。当我使用错误的Tiff标签打开.tiff文件时会出现此错误消息,在我的情况下标签如下:
PHOTOMETRIC_MINISBLACK = 1; NO(!)Extrasample 而该tiff是具有一个extrasample(掩码)的RGB图像。在此之前指定标签没有用,因为在实践中我不知道波段。我只想压制警告,但一直没有成功。我已经尝试过以下方法:
Python Warnings模块,抑制所有类型的警告 rasterio日志记录
这是我获取警告的方法:
tiff = rasterio.open(path)
img  = rast.read()

如果您想自行尝试,请在Google Drive中找到示例Tiff
有人知道如何通常抑制警告吗?
编辑: 这是我的rasterio版本信息pip show -v rasterio:
Name: rasterio
Version: 1.2.10
Summary: Fast and direct raster I/O for use with Numpy and SciPy
Home-page: https://github.com/mapbox/rasterio
Author: Sean Gillies
Author-email: sean@mapbox.com
License: BSD
Location: /home/david/miniconda3/lib/python3.9/site-packages
Requires: click-plugins, numpy, snuggs, cligj, click, setuptools, affine, certifi, attrs
Required-by: rioxarray
Metadata-Version: 2.1
Installer: pip
Classifiers:
  Development Status :: 5 - Production/Stable
  Intended Audience :: Developers
  Intended Audience :: Information Technology
  Intended Audience :: Science/Research
  License :: OSI Approved :: BSD License
  Programming Language :: C
  Programming Language :: Cython
  Programming Language :: Python :: 3.6
  Programming Language :: Python :: 3.7
  Programming Language :: Python :: 3.8
  Programming Language :: Python :: 3.9
  Programming Language :: Python :: 3
  Topic :: Multimedia :: Graphics :: Graphics Conversion
  Topic :: Scientific/Engineering :: GIS
Entry-points:
  [console_scripts]
  rio=rasterio.rio.main:main_group
  [rasterio.rio_commands]
  blocks=rasterio.rio.blocks:blocks
  bounds=rasterio.rio.bounds:bounds
  calc=rasterio.rio.calc:calc
  clip=rasterio.rio.clip:clip
 convert=rasterio.rio.convert:convert
  edit-info=rasterio.rio.edit_info:edit
  env=rasterio.rio.env:env
  gcps=rasterio.rio.gcps:gcps
  info=rasterio.rio.info:info
  insp=rasterio.rio.insp:insp
  mask=rasterio.rio.mask:mask
  merge=rasterio.rio.merge:merge
  overview=rasterio.rio.overview:overview
  rasterize=rasterio.rio.rasterize:rasterize
  rm=rasterio.rio.rm:rm
  sample=rasterio.rio.sample:sample
  shapes=rasterio.rio.shapes:shapes
  stack=rasterio.rio.stack:stack
  transform=rasterio.rio.transform:transform
  warp=rasterio.rio.warp:warp
Note: you may need to restart the kernel to use updated packages.

如果您有一个导致问题的奇怪、不兼容的TIFF文件,您需要共享它——也许可以使用Dropbox或者Google Drive。 - Mark Setchell
谢谢您的建议!我已相应地修改了问题。 :) - BananaJoe
权限不允许访问。 - Mark Setchell
现在应该可以工作了,抱歉。 - BananaJoe
1个回答

4

更新的答案

经过大量搜索,并尝试使用如此处所述的TIFFSetWarningHandler(),发现rasterio使用了自己内置的、精简版的gdal,除非你从源代码编译。这引出了以下方法。

方法1

告诉rasterio的GDAL消除警告:

#!/usr/bin/env python3

# Get cut-down GDAL that rasterio uses
from osgeo import gdal
# ... and suppress errors
gdal.PushErrorHandler('CPLQuietErrorHandler')

import rasterio

# Open TIFF and read it
tiff = rasterio.open('original.tiff')
img  = tiff.read()
print(img.shape)

示例输出
(4, 512, 512)    

我发现一些额外的东西,你可能想参考这里

方法2 - 使用tifffile

另一个选择可能是使用tifffile,因为它不会针对您的文件发出警告:
from tifffile import imread

img = imread('original.tiff')
print(img.shape)                 # prints (512, 512, 4)

这很简单且有效,但可能缺少完整的GeoTIFF阅读器的一些功能。
第三种方法 - 覆盖libtiff警告处理程序
这使用ctypes调用DLL /共享对象库并覆盖库的警告处理程序:
import ctypes
from ctypes.util import find_library

# Find the path to the library we want to modify
thePath = find_library('tiff')     # try "gdal" instead of "tiff" too
print(thePath)

# Get handle to it
theLib = ctypes.CDLL(thePath)
theLib.TIFFSetWarningHandler.argtypes = [ctypes.c_void_p]
theLib.TIFFSetWarningHandler.restype = ctypes.c_void_p
theLib.TIFFSetWarningHandler(None)

import rasterio

# Open TIFF and read it
tiff = rasterio.open('original.tiff')
img  = tiff.read()
print(img.shape)

你的TIFF文件不符合规范,因为它是RGBA格式,但“Photometric Interpretation”设置为“MIN_IS_BLACK”,表示它是灰度或双级色彩。与其抑制警告,我认为更好的选择是通过将“Photometric Interpretation”设置为“RGB”并将额外样本的类型设置为“UNASSOCIATED_ALPHA”来纠正你的TIFF文件。你可以使用随附于libtiff的tiffset命令完成此操作。
tiffset -s 262 2 11_369_744_2022-10-18.tiff       # Photometric = RGB
tiffset -s 338 1 2 11_369_744_2022-10-18.tiff     # Extra sample is UNASSOCIATED_ALPHA

“Libtiff”现在在macOS的“预览”中加载您的图像时不再生成错误,并以RGB颜色显示。
在原始图像上运行“tiffinfo”命令会得到:
TIFFReadDirectory: Warning, Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples..
=== TIFF directory 0 ===
TIFF Directory at offset 0x8 (8)
  Image Width: 512 Image Length: 512
  Resolution: 1, 1 (unitless)
  Bits/Sample: 8
  Compression Scheme: Deflate
  Photometric Interpretation: min-is-black
  Samples/Pixel: 4
  Rows/Strip: 8
  Planar Configuration: single image plane
  Tag 33550: 76.437028,76.437028,0.000000
  Tag 33922: 0.000000,0.000000,0.000000,9079495.967826,5596413.462927,0.000000
  Tag 34735: 1,1,0,12,1024,0,1,1,1025,0,1,1,2050,0,1,1,1026,34737,37,0,2049,34737,6,38,2054,0,1,9102,2056,0,1,1,2057,34736,1,0,2058,34736,1,1,2061,34736,1,2,3072,0,1,3857,3076,0,1,9001
  Tag 34736: 6378137.000000,6378137.000000,0.000000
  Tag 34737: Popular Visualisation Pseudo Mercator|WGS 84|

在带有更正标签的图像上:
=== TIFF directory 0 ===
TIFF Directory at offset 0x99858 (628824)
  Image Width: 512 Image Length: 512
  Resolution: 1, 1 (unitless)
  Bits/Sample: 8
  Compression Scheme: Deflate
  Photometric Interpretation: RGB color
  Extra Samples: 1<unassoc-alpha>
  Samples/Pixel: 4
  Rows/Strip: 8
  Planar Configuration: single image plane
  Tag 33550: 76.437028,76.437028,0.000000
  Tag 33922: 0.000000,0.000000,0.000000,9079495.967826,5596413.462927,0.000000
  Tag 34735: 1,1,0,12,1024,0,1,1,1025,0,1,1,2050,0,1,1,1026,34737,37,0,2049,34737,6,38,2054,0,1,9102,2056,0,1,1,2057,34736,1,0,2058,34736,1,1,2061,34736,1,2,3072,0,1,3857,3076,0,1,9001
  Tag 34736: 6378137.000000,6378137.000000,0.000000
  Tag 34737: Popular Visualisation Pseudo Mercator|WGS 84|

嗨,马克,感谢你的帮助。但不幸的是,这对我没有用,因为我通常不知道我得到什么样的图像(可能只有一个样本,甚至十个样本,即任何类型的卫星传感器数据都可以是样本)。 - BananaJoe
我进一步查找了一下并更新了我的答案。 - Mark Setchell
再次感谢您的时间。对我来说似乎不起作用,我在问题中添加了代码片段和驱动器中的警告截图。 - BananaJoe
已添加。 :) - BananaJoe
我尝试了所有三种方法,只有第二种在我的情况下有效(不知道为什么第三种不起作用),一个“解决方案”是使用rasterio.open()加载地理数据和使用imread加载图像数据。 - BananaJoe
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接