title: Creating a tiff stack from individual tiffs in python
body: <p>I am creating tiff stacks of different sizes based on the example found here: 
  <a href="http://www.bioimgtutorials.com/2016/08/03/creating-a-z-stack-in-python/" rel="nofollow noreferrer">http://www.bioimgtutorials.com/2016/08/03/creating-a-z-stack-in-python/</a></p>
  
  <p>A sample of the tif files can be downloaded here:
  <a href="https://drive.google.com/open?id=12CuVWWUs9horuNIcbKnsOYrYJnWLb9pM" rel="nofollow noreferrer">nucleus</a></p>
  
  <p>I have a folder with 5 tiff files inside.
  I want to stack them to be able to open them in imageJ so that they look like this:</p>
  
  <p><a href="https://i.stack.imgur.com/rQe06.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rQe06.jpg" alt="enter image description here"></a></p>
  
  <p>And this works with the following code:</p>
  
  <pre><code>from skimage import io
  import numpy as np
  import os
  
  dir = 'C:/Users/Mich/Desktop/tiff stack/'
  
  listfiles =[]
  
  
  for img_files in os.listdir(dir):
      if img_files.endswith(".tif") :
          listfiles.append(img_files)
  
  
  
  first_image = io.imread(dir+listfiles[0])
  
  io.imshow(first_image)
  
  first_image.shape
  
  stack = np.zeros((5,first_image.shape[0],first_image.shape[1]),np.uint8)
  
  
  
  for n in range(0,5):
      stack[n,:,:]= io.imread(dir+listfiles[n])
  
  path_results = 'C:/Users/Mich/Desktop/'
  io.imsave(path_results+'Stack.tif' ,stack)
  </code></pre>
  
  <p>The problem comes when I just want to stack the 4 first ones or the 3 first ones.</p>
  
  <p>Example with 4 tiff images:</p>
  
  <pre><code>   stack=np.zeros((4,first_image.shape[0],first_image.shape[1]),np.uint8)
  
     for n in range(0,4):
         stack[n,:,:]= io.imread(dir+listfiles[n])
  </code></pre>
  
  <p>Then I obtain this kind of result:
  <a href="https://i.stack.imgur.com/2B1k8.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2B1k8.jpg" alt="enter image description here"></a></p>
  
  <p>and while trying to stack the 3 first images of the folder, they get combined!</p>
  
  <pre><code>   stack=np.zeros((3,first_image.shape[0],first_image.shape[1]),np.uint8)
  
     for n in range(0,3):
         stack[n,:,:]= io.imread(dir+listfiles[n])
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/YlJ4c.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YlJ4c.jpg" alt="enter image description here"></a></p>
  
  <p>Where am I wrong in the code, so that it dosent just add the individual tiff in a multidimensional stack of the sizes 3, 4 or 5 ?</p>