2014-10-28 46 views
0

我想要做類似這樣的帖子的內容: sample code浮動圖片的Python的平均圖片

我如何改變給定的代碼,以在「TIFF」浮動圖片的工作 - 格式?

這是我的嘗試:

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() 

至極產生這個錯誤:

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) 

我不熟悉numpy的陣列,因此任何幫助是值得歡迎的。

回答