title: Average Picture of float Pictures Python
body: <p>I want to do something similar to this post: <a href="https://stackoverflow.com/questions/17291455/how-to-get-an-average-picture-from-100-pictures-using-pil">sample code</a>.</p>
  <p>How can I change the given code to work for float images in &quot;tiff&quot;- format?</p>
  <p>this is what I tried:</p>
  <pre><code>import os, numpy, PIL
  from PIL import Image
  
  # Access all PNG files in directory
  os.chdir(&quot;c://users//Student///Desktop//bilder//&quot; )
  print &quot;Current working dir : %s&quot; % os.getcwd()
  
  allfiles=os.listdir(os.getcwd())
  imlist=[filename for filename in allfiles if  filename[-5:] in [&quot;.tiff&quot;,&quot;.tiff&quot;]]
  #imlist=[filename for filename in allfiles]
  
  # Assuming all images are the same size, get dimensions of first image
  w,h=Image.open(imlist[0]).size
  N=len(imlist)
  
  # Create a numpy array of floats to store the average (assume RGB images)
  arr=numpy.zeros((h,w,1),numpy.float)
  
  # Build up average pixel intensities, casting each image as an array of floats
  for im in imlist:
      imarr=numpy.array(Image.open(im),dtype=numpy.float)
      arr+=imarr/N
  
  # Round values in array and cast as 8-bit integer
  arr=numpy.array(numpy.round(arr),dtype=numpy.uint8)
  
  # Generate, save and preview final image
  out=Image.fromarray(arr,mode=&quot;F&quot;)
  out.save(&quot;Average.tiff&quot;)
  out.show()
  </code></pre>
  <p>which produces this error:</p>
  <pre><code>File &quot;C:\Users\Student\im_average.py&quot;, line 25, in &lt;module&gt;
      arr+=imarr/N
  ValueError: non-broadcastable output operand with shape (250,250,1) doesn't match the broadcast shape (250,250,250)
  </code></pre>
  <p>I am not familiar with numpy arrays, so any help is welcome.</p>