Merge individual tiff files into Z-stacks and find maximum intensity projection

I want merge multiple .tif files into one Z-stack and then find the maximum intensity projection. I’m using the tifffile library but am open to any better alternatives.
I’m having difficulty finding the maximum projection.

# Merge individual .tif images into Z-stacks

import glob
import tifffile as tiff
import numpy as np
from skimage import io
import matplotlib.pyplot as plt

with tiff.TiffWriter('Stack.tif') as stack:
    for filename in glob.glob('./HTAN/CODEX_images/*.tif'):
        stack.save(
            tiff.imread(filename), 
            photometric='minisblack', 
            contiguous=True
        )
        
        img_max = np.max(filename.astype(np.float), axis=0)
        plt.imshow(img_max)


> --------------------------------------------------------------------------- AttributeError                            Traceback (most recent call
> last) /tmp/ipykernel_13470/868041214.py in <module>
>      15         )
>      16 
> ---> 17         img_max = np.max(filename.astype(np.float)[1], axis=0)
>      18         plt.imshow(img_max)
> 
> AttributeError: 'str' object has no attribute 'astype'

Source: Python Questions

LEAVE A COMMENT