【问题标题】:Numpy / Scipy not writing to certain pixelsNumpy / Scipy 不写入某些像素
【发布时间】:2020-01-21 22:40:08
【问题描述】:

我正在尝试使用 numpy 和 scipy 创建一个 png 图像,然后分别为每个像素着色。但是,当我选择大小 255 x 255 并尝试更改 x,y 坐标 255,255 处的像素时,它说它超出了范围。

import numpy as np
import scipy.misc as smp
from PIL import Image

data = np.zeros( (255,255), dtype = np.uint8)
data[255,255] = [0,0,0] # Attempt to set a black pixel
data[0,0] = [255,0,0] # Attempt to set a red pixel
img = Image.fromarray(data)
img.save("Test.png","PNG")

但是,我得到以下错误代码:

Traceback (most recent call last):
  File "C:\Users\***\***\***\***\***.py", line 8, in <module>
    data[255,255] = [255,0,255]
IndexError: index 255 is out of bounds for axis 0 with size 255

如果有任何帮助或建议,我将不胜感激,谢谢。

【问题讨论】:

  • NumPy 索引(就像 Python 索引)是从零开始的。如果大小为 255,则索引范围为 0 到 254。
  • @WarrenWeckesser 你不会相信我只是用力捂脸。谢谢你没有因为我的白痴而居高临下......也许把它作为答案让我可以正确接受?

标签: python numpy scipy


【解决方案1】:

NumPy 索引(就像 Python 索引)是从零开始的。如果大小为 255,则索引范围为 0 到 254。

  • 感谢 Warren Wecknesser

【讨论】:

    猜你喜欢
    • 2016-09-11
    • 2020-05-22
    • 2018-07-26
    • 2013-10-23
    • 1970-01-01
    • 2020-02-13
    • 2022-01-26
    • 1970-01-01
    • 2013-03-09
    相关资源
    最近更新 更多