2011-07-21 145 views
12

我試圖創建一組縮略圖,每個圖像都是從原始圖像中單獨縮減的。PIL圖像對象上的Python副本

image = Image.open(path) 
image = image.crop((left, upper, right, lower)) 
for size in sizes: 
    temp = copy.copy(image) 
    temp.thumbnail((size, height), Image.ANTIALIAS) 
    temp.save('%s%s%s.%s' % (path, name, size, format), quality=95) 

上面的代碼似乎正常工作,但在測試我發現有些圖像(我也說不清是什麼特別之處他們,也許只爲PNG)提出這個錯誤:

/usr/local/lib/python2.6/site-packages/PIL/PngImagePlugin.py in read(self=<PIL.PngImagePlugin.PngStream instance>) 
line: s = self.fp.read(8) 
<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'read' 

copy()這些圖像工作得很好。

我可以爲每個縮略圖重新打開並裁剪圖像,但我寧願有一個更好的解決方案。

回答

32

我猜copy.copy()不適用於PIL Image類。嘗試使用Image.copy()代替,因爲它是有原因的:

​​
+0

謝謝,這是有效的。 – Steffen