2013-03-25 72 views
1

當我運行這段代碼我得到的輸出:數據類型錯誤

TypeError: an integer is required 

我不知道爲什麼會這樣,因爲我同時設置了數據類型分別UINT8 UINT64和。顯然我不太瞭解數據類型。

from PIL import Image 

from numpy import random 

N = 100 

##open an image 
im=Image.open('/Users/grahamwarner/Desktop/Experiment/gwarner/labeled_photos/masks/003030.png') 

##create a random image 
rand_matrix = random.randint(0, 255, (500, 500, 3)).astype('uint8') 

rand_image = Image.fromarray(rand_matrix) 

##select N random pixels 
rand_pix = random.randint(0,499, (N,2)).astype('uint64') 

##replace the random values at these pixels with the original values 
for ii in range(N): 

    rand_image.putpixel(tuple(rand_pix[ii,:]), im.getpixel(tuple(rand_pix[ii,:]))) 
+0

哪一行發生了? – CppLearner 2013-03-25 18:27:30

+0

如果此問題已得到解答,則應接受最佳答案,請參閱[常見問題解答#howtoask]。 – askewchan 2013-04-16 16:12:39

回答

1

getpixel該方法在PIL似乎是繞其輸入非常挑剔,特別希望的int秒(其是不一樣的numpy的的uint64型)的元組。以下適用於我:

for ii in range(N): 
    coordinate = tuple(map(int, rand_pix[ii,:])) 
    rand_image.putpixel(coordinate, im.getpixel(coordinate))