我有一个 500x500 数组,我正在尝试编写一个名为“坐标”的变量,它将挑选出每个值,然后将其应用于函数,但我不断得到输出,
AttributeError Traceback (most recent call last)
/home/graham/<ipython-input-17-cc6ce0649eda> in <module>()
31 pass
32
---> 33 finished_array = rand_array_color(rand_array)
34
35 finished_image = Image.fromarray(finished_array)
/home/graham/<ipython-input-17-cc6ce0649eda> in rand_array_color(rand_array)
23 from PIL import Image
24 for ii in numpy.nditer(rand_array):
---> 25 coordinate = tuple(map(int, rand_image[ii,:]))
26 if ii < 128:
27 print "false"
/usr/lib/python2.7/dist-packages/PIL/Image.pyc in __getattr__(self, name)
510 new['data'] = self.tostring()
511 return new
--> 512 raise AttributeError(name)
513
514 ##
AttributeError: __getitem__
这是我的代码
from PIL import Image
from numpy import random
im = Image.open('/home/graham/Desktop/Experiment/gwarner/labeled_photos/photos/003030.png')
rand_array = numpy.random.randint(255, size=(500,500)).astype('uint8')
rand_image = Image.fromarray(rand_array)
def rand_array_color(rand_array):
from PIL import Image
for ii in numpy.nditer(rand_array):
coordinate = tuple(map(int, rand_image[ii,:]))
if ii < 128:
newvalue = rand_image.putpixel(coordinate(a,ii), im.getpixel(coordinate(a,ii)))
return newvalue
else:
pass
finished_array = rand_array_color(rand_array)
我也一直在摆弄另一个版本的坐标,
coordinate = tuple(int(rand_array[ii,0]),int(rand_array[ii,1])
但它只是返回,
NameError: name 'ii' is not defined
谁能告诉我如何解决其中一个问题或推荐另一种可行的方法?