def getIndex_fromfilename(imagefilename, nbdigits=4, CCDLabel=None,
    stackimageindex=-1):
    """
    get integer index from imagefilename string

    :param imagefilename: filename string (full path or not)

    :return: file index
    """
    if CCDLabel in ('sCMOS', 'sCMOS_fliplr'):
        ext = 'tif'
        lenext = 4
        if imagefilename.endswith('tiff'):
            ext = 'tiff'
            lenext = 5
        print(imagefilename)
        if nbdigits is not None:
            if imagefilename.endswith(ext):
                imageindex = int(imagefilename[-(lenext + nbdigits):-lenext])
            elif imagefilename.endswith(ext + '.gz'):
                imageindex = int(imagefilename[-(lenext + 3 + nbdigits):-(
                    lenext + 3)])
        elif imagefilename.endswith(ext):
            prefix, _ = imagefilename.split('.')
            imageindex = int(prefix.split('_')[1])
    elif CCDLabel in ('EIGER_4Mstack',):
        imageindex = stackimageindex
    elif imagefilename.endswith('mar.tiff'):
        imageindex = int(imagefilename[-(9 + nbdigits):-9])
    elif imagefilename.endswith('tiff'):
        imageindex = int(imagefilename[-(5 + nbdigits):-5])
    elif imagefilename.endswith('mar.tif'):
        imageindex = int(imagefilename.split('_')[-2])
    elif imagefilename.endswith('.tif'):
        imageindex = int(imagefilename.split('_')[-1][:-4])
    elif imagefilename.endswith('mccd'):
        imageindex = int(imagefilename[-(5 + nbdigits):-5])
    elif imagefilename.endswith('edf'):
        imageindex = int(imagefilename[-(4 + nbdigits):-4])
    elif imagefilename.endswith('unstacked'):
        imageindex = int(imagefilename[-(10 + nbdigits):-10])
    return imageindex
