首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >抑制RasterIO警告。警告1: TIFFReadDirectory

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

Stack Overflow用户
提问于 2022-10-16 17:09:50
回答 1查看 167关注 0票数 0

错误消息Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.一直存在问题--当我打开带有错误tiff标记的.tiff文件时,就会发生错误消息,在我的示例中,这些标记:

  • PHOTOMETRIC_MINISBLACK = 1;
  • 不(!)实例

而tiff是一个带有一个额外(掩码)的RGB图像。在打开之前指定标签在这里是没有用的,因为我在实践中不知道乐队。我只是想压制一下这个警告,但没有成功。我已经尝试了以下几种方法:

  • 模块,禁止所有类型的警告。
  • rasterio测井

我就是这么做来得到警告的:

代码语言:javascript
复制
tiff = rasterio.open(path)
img  = rast.read()

如果你想自己尝试一下,你可以在Google中找到一个例子蒂夫

有人知道如何一般地压制警告吗?

编辑:这是关于我的rasterio版本pip show -v rasterio的信息

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

发布于 2022-10-20 07:48:06

最新答案

经过大量搜索,并试图使用TIFFSetWarningHandler()描述的这里,发现rasterio使用了自己的、内置的、精简的gdal版本--除非您是从源代码构建的。这就产生了以下情况。

方法1

告诉rasterio的GDAL安静警告:

代码语言:javascript
复制
#!/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)

样本输出

代码语言:javascript
复制
(4, 512, 512)    

我发现了一些您可能想要参考的其他内容-- 这里

方法2-使用tifffile

另一种选择可能是使用tifffile,因为它不会为您的文件发出警告:

代码语言:javascript
复制
from tifffile import imread

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

这是简单而有效的,但可能缺乏完整的GeoTIFF阅读器的一些功能。

方法3-重写libtiff警告处理程序

这使用ctypes调用DLL/共享对象库,并覆盖库的警告处理程序:

代码语言:javascript
复制
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,但是将“光度解释”设置为MIN_IS_BLACK,这意味着它是灰度或双级别的。

与其消除警告,更好的选择IMHO是通过将“光度解释”设置为UNASSOCIATED_ALPHA. RGB并将额外示例的类型设置为来纠正TIFF。

您可以使用tiffset ( libtiff附带的)来实现这一点。

代码语言:javascript
复制
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提供:

代码语言:javascript
复制
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|

在带有校正标签的图像上:

代码语言:javascript
复制
=== 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|
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74089170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档