I want to do something similar to this post: sample code.
How can I change the given code to work for float images in "tiff"- format?
this is what I tried:
import os, numpy, PIL
from PIL import Image
# Access all PNG files in directory
os.chdir("c://users//Student///Desktop//bilder//" )
print "Current working dir : %s" % os.getcwd()
allfiles=os.listdir(os.getcwd())
imlist=[filename for filename in allfiles if filename[-5:] in [".tiff",".tiff"]]
#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="F")
out.save("Average.tiff")
out.show()
which produces this error:
File "C:\Users\Student\im_average.py", line 25, in <module>
arr+=imarr/N
ValueError: non-broadcastable output operand with shape (250,250,1) doesn't match the broadcast shape (250,250,250)
I am not familiar with numpy arrays, so any help is welcome.