2

I've been having problems with the error message Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples. This error message occurs when I open a .tiff file with wrong tiff tags, in my case the tags:

  • PHOTOMETRIC_MINISBLACK = 1;
  • NO(!) Extrasample

while the tiff is a RGB image with one extrasample (mask). To specify the tags before opening has no use here, since I don't know the bands in practice. I simply want to suppress the warning but haven't had success with it. I already tried the following:

  • python Warnings module, suppressing all kind of warnings
  • rasterio logging

This is what I do to get the Warning:

tiff = rasterio.open(path)
img  = rast.read()

If you want to try it out yourself, you can find an example Tiff in the Google Drive.

Does someone know how to generally suppress the warning?

EDIT: Here is the information about my rasterio version 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.
BananaJoe
  • 55
  • 6

1 Answers1

3

Updated Answer

After a lot of hunting around, and trying to use TIFFSetWarningHandler() as described here, it transpires that rasterio uses its own, built-in, stripped-down version of gdal - unless you build from source. That gives rise to the following.

Method 1

Tell rasterio's GDAL to quiet warnings:

#!/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)

Sample Output

(4, 512, 512)    

I found some additional stuff you may like to refer to here.

Method 2 - Use tifffile

Another option might be to use tifffile instead as it doesn't emit warnings for your file:

from tifffile import imread

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

This is simple and effective but may lack some features of a full GeoTIFF reader.

Method 3 - Override the libtiff warning handler

This uses ctypes to call into the DLL/shared object library and override the library's warning handler:

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)

Original Answer

Your TIFF is non-compliant because it is RGBA but has "Photometric Interpretation" set to MIN_IS_BLACK meaning it is greyscale or bi-level.

Rather than suppressing warnings, the better option IMHO, is to correct your TIFF by setting the "Photometric Interpretation" to RGB and setting the type of the extra sample to UNASSOCIATED_ALPHA.

You can do that with tiffset which comes with libtiff:

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 now no longer generates errors when loading your image and it displays as RGB colour on macOS Preview.


Running tiffinfo on the original image gives:

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|

And on the image with corrected tags:

=== 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|
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Hi Mark, thank you for your help. But sadly this doesn't work for me, since I normally don't know what kind of image I get (could be just one sample or even ten samples i.e. - a sample being any kind of satellite sensor data). – BananaJoe Oct 20 '22 at 08:13
  • I snuffled around further and have updated my answer. – Mark Setchell Oct 20 '22 at 10:01
  • Again thank you for your time. It doesn't seem to work for me, I added a Screenshot of the code snippet and Warning in the Drive found in the question. – BananaJoe Oct 20 '22 at 10:42
  • Maybe you installed from source and therefore are using a full `gdal`? Can you run `pip show -v rasterio` and add the output to your question please? – Mark Setchell Oct 20 '22 at 10:53
  • It is added. :) – BananaJoe Oct 20 '22 at 11:13
  • You could try looking for `gdal` or `tiff` libraries inside your conda installation `find /home/david/miniconda3 | grep -E "gdal|tiff" | grep -E "dylib|so"` and try Method 3 in my answer. – Mark Setchell Oct 20 '22 at 11:32
  • I tried all three methods, only the second one works in my case (no idea why number 3 is not working), one "solution" would be to load in the geo-data with `rasterio.open()` and the image data with `imread`. – BananaJoe Oct 20 '22 at 12:19