2016-07-24 18 views
0

PIL如何處理seek()函數以在多幀.tiff文件中操作?我試圖提取文件中各種幀的一段信息(灰度像素值),但無論我設置了哪個搜索,都會引發EOFE錯誤。 示例代碼:使用seek()創建一個以.tiff格式比較軟件的平均像素值?

from PIL import Image 
im = Image.open('example_recording.tif').convert('LA') 

width,height = im.size 
image_lookup = 0 
total=0 
for i in range(0,width): 
    for j in range(0,height): 
     total += im.getpixel((i,j))[0] 

total2=0 
im.seek(1) 
for i in range(0,width): 
    for j in range(0,height): 
     total += im.getpixel((i,j))[0] 

print total 
print total2 

錯誤日誌看起來是這樣的:

文件 「C:\用戶\ ltopuser \ Anaconda2 \ LIB \站點包\ PIL \ Image.py」,線1712,在尋求 加薪的EOFError

的EOFError

乾杯,JJ

+1

請發表您的答案作爲答案,並接受它,如果它確實回答你的問題。 –

回答

1

由PIL越來越結束文件的原因造成的:可以是固定的這樣;

class ImageSequence: 
def __init__(self, im): 
    self.im = im 
def __getitem__(self, ix): 
    try: 
     if ix: 
      self.im.seek(ix) 
     return self.im 
    except EOFError: 
     raise IndexError # this is the end of sequence 

for frame in ImageSequence(im): 
for i in range(0,width): 
    for j in range(0,height): 
     total += im.getpixel((i,j))[0]