imagestack.py 1021 Bytes
import glob
import imageio
import numpy
import progressbar

def loadstack(fmask, dtype=numpy.float32):
    #read the input files
    F = glob.glob(fmask)
    if(len(F)==0):
        print("No image files found.")
        return
    
    #load the first file
    I = imageio.imread(F[0])
    
    #generate the image stack
    S = numpy.zeros((len(F), I.shape[0], I.shape[1], I.shape[2]), dtype)
    
    bar = progressbar.ProgressBar(max_value=len(F))
    
    for i in range(0, len(F)):
        I = imageio.imread(F[i])
        S[i, :, :, :] = I
        bar.update(i+1)
        
    return S

def rgb2gray(rgb):
    r, g, b = rgb[...,0], rgb[...,1], rgb[...,2]
    gray = 0.2989 * r + 0.5870 * g + 0.1140 * b

    return gray

def savestack(I, fname, extension = ".bmp"):
    #output the final images
    bar = progressbar.ProgressBar(max_value=I.shape[0])
    #save the output files
    for i in range(0, I.shape[0]):        
        imageio.imwrite(fname + "%03d" % i + extension, I[i, :, :, :])
        bar.update(i+1)