Пример #1
0
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()
    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


def parseNextIntBigEndian(ra, count):
    # ra.read() reads a single byte as an unsigned int
    return BigInteger([ra.readByte() for _ in xrange(count)]).intValue()


def parseNextIntLittleEndian(ra, count):
    # ra.read() reads a single byte as an unsigned int
    return BigInteger(reversed([ra.readByte()
                                for _ in xrange(count)])).intValue()