TypeError: 'Image' object is not subscriptable
TypeError: 'Image' object is not subscriptable
正在尝试创建高度图,但不断收到 TypeError 提示 Image 对象不可订阅。不知道为什么。
pixels = Image.new('RGB', (1789, 1789), color = 'red')
for i in range(pixels.size[0]):
for j in range(pixels.size[1]):
if pixels[i,j] != (255, 0, 0):
pixels[i,j] = (0, 0 ,0)
要获得单个像素,请使用 pixels.getpixel((i,j))
。请注意,这是一种访问图像的非常缓慢的方法。通常最好使用 tobytes
转换为字节列表,然后使用 frombytes
将其转换回来。
当然,你的例子很愚蠢。每个像素都是 (255,0,0)。
正在尝试创建高度图,但不断收到 TypeError 提示 Image 对象不可订阅。不知道为什么。
pixels = Image.new('RGB', (1789, 1789), color = 'red')
for i in range(pixels.size[0]):
for j in range(pixels.size[1]):
if pixels[i,j] != (255, 0, 0):
pixels[i,j] = (0, 0 ,0)
要获得单个像素,请使用 pixels.getpixel((i,j))
。请注意,这是一种访问图像的非常缓慢的方法。通常最好使用 tobytes
转换为字节列表,然后使用 frombytes
将其转换回来。
当然,你的例子很愚蠢。每个像素都是 (255,0,0)。