当我运行此代码时,我得到输出:
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,:])))