def dimensionsOf(path): fr = None try: fr = ChannelSeparator() fr.setGroupFiles(False) fr.setId(path) return fr.getSizeX(), fr.getSizeY() except: print sys.exc_info() finally: fr.close()
from loci.formats import ChannelSeparator import os, sys # Read the dimensions of the image at path by parsing the file header only, # thanks to the LOCI Bioformats library filepath = "/home/albert/Desktop/t2/bat-cochlea-volume.tif" try: fr = ChannelSeparator() fr.setGroupFiles(False) fr.setId(filepath) width, height, nSlices = fr.getSizeX(), fr.getSizeY(), fr.getSizeZ() n_pixels = width * height * nSlices bitDepth = fr.getBitsPerPixel() fileSize = os.path.getsize(filepath) headerSize = fileSize - (n_pixels * bitDepth / 8) # 8 bits in 1 byte print "Dimensions:", width, height, nSlices print "Bit depth: %i-bit" % bitDepth print "Likely header size, in number of bytes:", headerSize except: # Print the error, if any print sys.exc_info() finally: fr.close() # close the file handle safely and always # For parsing TIFF file headers from java.io import RandomAccessFile from java.math import BigInteger