Search code examples
tifflibtiff

how to determine if a tiff was written in bigtiff format


BigTIFF isn't supported in all image readers, and I'd like to identify files based on their TIFF format. How can I determine whether an existing tiff file was written in the standard TIFF format, or instead uses the BigTIFF format?


Solution

  • The python library tifffile reads TIFF files, and one of the attributes indicates the format of the TIFF.

    filename = '/path/to/filename.tif'
    import tifffile
    def is_bigtiff(filename):
        with tifffile.TiffFile(f) as img:
            return img.is_bigtiff
    

    tifffile is just reading the first few bytes of the tiff header to determine whether it is BigTIFF, so the logic is easy to recreate without the whole library (which I just realized upon looking at the source for the library, and from which the following is drawn).

    import struct
    
    def is_bigtiff(filename):
        with open(filename, 'rb') as f:
            header = f.read(4)
        byteorder = {b'II': '<', b'MM': '>', b'EP': '<'}[header[:2]]
        version = struct.unpack(byteorder + "H", header[2:4])[0]
        return version == 43