正如标题所说,我试图用Python定义一个矩阵数组(witch代表图像)。但是,当我试图读取一个矩阵时,我得到了这样一条消息:“ImageData实例没有属性'getitem”
最近我开始学习Python,所以我知道这对很多人来说一定很简单,但我不知道出了什么问题。这是我的代码:
在图像数据.py在import random
import math
class ImageData:
def __init__ (self, width, height):
self.width = width
self.height = height
self.data = []
for i in range(width):
self.data.append([0] * height)
def set_data (self, x, y, value):
self.data[x][y] = value
def generate_voronoi_diagram (self, seeds):
nx = []
ny = []
nr = []
ng = []
nb = []
for i in range(seeds):
# Generate a cell position
pos_x = random.randrange(self.width)
pos_y = random.randrange(self.height)
nx.append(pos_x)
ny.append(pos_y)
# Save the rgb data
nr.append(random.randrange(256))
ng.append(random.randrange(256))
nb.append(random.randrange(256))
for x in range(self.width):
for y in range(self.height):
# Return the Euclidean norm
d_min = math.hypot(self.width-1, self.height-1)
j = -1
for i in range(seeds):
# The distance from a cell to x, y point being considered
d = math.hypot(nx[i]-x, ny[i]-y)
if d < d_min:
d_min = d
j = i
self.data[x][y] = [nr[j], ng[j], nb[j]]
北卡罗来纳州确定可视化.py在
^{pr2}$
任何帮助都将不胜感激。
谢谢。在