Geographic Information Systems Asked on April 5, 2021
Is there a way to find the size (in bytes) of the header information in a GeoTIFF file using Python? I am not trying to read the header (I know I can use gdalinfo
for this) but rather to figure out what position I need to skip to in order to read the file as binary. This is because I am interested in testing numpy memory-mapped arrays for reading portions of an image from the disk. I would like to test this versus the gdal builtin ReadAsArray()
method because I enjoy the flexibility of numpy indexing.
Check out tifffile, which is a Python package to read and write image data from and to TIFF files.
import tifffile
import numpy as np
fname = 'my.tif'
tif = tifffile.TiffFile(fname)
page = tif.pages[0] # first page
arr = page.asarray() # we're done
So far, this is no different than either rasterio or GDAL.
To answer your question, you can get the byte offset and size from the page.is_contiguous
property, then read it with regular numpy or any other tool that can read contiguous data:
offset, byte_count = page.is_contiguous
with open(fname, 'rb') as fp:
fp.seek(offset)
# if this is a 4-byte float file ...
arr2 = np.fromfile(fp, dtype=np.float32, count=byte_count / 4)
arr2.shape = arr.shape
assert (arr2 == arr).all()
Correct answer by Mike T on April 5, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP