2010-02-26 50 views

回答

9

getdata()的輸出不包含圖片格式或大小,所以您需要保留這些信息(或以其他方式獲取信息)。然後做到這一點,利用putdata()方法:

# get data from old image (as you already did) 
data = list(oldimg.getdata()) 

# create empty new image of appropriate format 
newimg = Image.new(format, size) # e.g. ('RGB', (640, 480)) 

# insert saved data into the image 
newimg.putdata(data) 
+0

對於我的情況,我知道圖像尺寸先驗,所以我並沒有包括在我的問題在於細節。我同意你對此的評論是有效的,畢竟列表是一維的,所以必須告訴PIL內部如何將列表當作二維數組處理。謝謝。 – 2010-02-26 19:10:39