def bit_tester(image_name): ## gets the number of bits for an image, error if not 8 or 16 bit. ## dest = IJ.getDirectory("image") filepath = dest + image_name fr = ChannelSeparator() fr.setId(filepath) bitDepth = fr.getBitsPerPixel() if int(bitDepth) in (8, 16): if int(bitDepth) == 8: bit_depth = 2**(bitDepth)-1 if int(bitDepth) == 16: bit_depth = 2**(bitDepth)-1 return bit_depth elif int(bitDepth) not in (8, 16): print("Error image is not 8 or 16 bit") quit()
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