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:
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:
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: [email protected]
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.
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.
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.
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.
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|