pygame.PixelArray
pygame object for direct pixel access of surfaces
PixelArray(Surface) -> PixelArray
Gets the Surface the PixelArray uses.
Returns the byte size of a pixel array item
Returns the number of dimensions.
Returns the array size.
Returns byte offsets for each array dimension.
Creates a new Surface from the current PixelArray.
Replaces the passed color in the PixelArray with another one.
Extracts the passed color from the PixelArray.
Compares the PixelArray with another one.
Exchanges the x and y axis.
Closes the PixelArray, and releases Surface lock.

PixelArray包装一个Surface,并提供对该Surface的像素的直接访问。像素阵列可以是一维或二维的。与其表面一样,二维数组也被编入索引 [列、行] 。像素数组支持切片,无论是用于返回子数组还是用于赋值。在单个列或行上切片的像素阵列返回一维像素阵列。不支持算术和其他运算。像素阵列可以安全地分配给它自己。最后,像素数组导出数组结构接口,允许它们与 pygame.pixelcopypygame module for general pixel array copying 方法和NumPy数组。

可以为PixelArray像素项分配一个原始整数值,即 pygame.Colorpygame object for color representations 实例,或a(r,g,b [, a] )元组。

pxarray[x, y] = 0xFF00FF
pxarray[x, y] = pygame.Color(255, 0, 255)
pxarray[x, y] = (255, 0, 255)

但是,只返回像素的整数值。因此,要将像素与特定颜色进行比较,需要首先使用 Surface.map_rgb() 为其创建PixelArray的Surface对象的方法。

pxarray = pygame.PixelArray(surface)
# Check, if the first pixel at the topleft corner is blue
if pxarray[0, 0] == surface.map_rgb((0, 0, 255)):
    ...

当指定给一定范围的像素时,可以使用非元组颜色序列或PixelArray作为值。对于序列,长度必须与PixelArray宽度匹配。

pxarray[a:b] = 0xFF00FF                   # set all pixels to 0xFF00FF
pxarray[a:b] = (0xFF00FF, 0xAACCEE, ... ) # first pixel = 0xFF00FF,
                                          # second pixel  = 0xAACCEE, ...
pxarray[a:b] = [(255, 0, 255), (170, 204, 238), ...] # same as above
pxarray[a:b] = [(255, 0, 255), 0xAACCEE, ...]        # same as above
pxarray[a:b] = otherarray[x:y]            # slice sizes must match

对于PixelArray赋值,如果右侧数组的行长为1,则在目标数组的行上广播该列。高度为1的数组在目标的列上广播,相当于指定一个1D PixelArray。

下标切片还可用于指定给目标PixelArray的矩形子视图。

# Create some new PixelArray objects providing a different view
# of the original array/surface.
newarray = pxarray[2:4, 3:5]
otherarray = pxarray[::2, ::2]

下标切片还可用于进行快速的矩形像素操作,而不是在x或y轴上迭代。这个

pxarray[::2, :] = (0, 0, 0)               # Make even columns black.
pxarray[::2] = (0, 0, 0)                  # Same as [::2, :]

在其生存期内,PixelArray锁定曲面,因此一旦不再使用它,您就必须显式地关闭()它,并且曲面应该在相同的作用域中执行操作。最好将其用作上下文管理器,并使用with PixelArray(Surf)作为PixelARRAY:Style。因此,它也适用于PYPY。

一个简单的 : 可以省略列的切片索引。

pxarray[::2, ...] = (0, 0, 0)             # Same as pxarray[::2, :]
pxarray[...] = (255, 0, 0)                # Same as pxarray[:]

有关像素数组到像素数组分配的说明,对于项大小为3(从24位表面创建)的数组,像素值将从源格式转换为目标格式。每个像素的红、绿和蓝颜色元素被移位以匹配目标表面的格式。对于所有其他像素大小,不会发生这样的重新映射。这一点应该会在以后的pyGame版本中改变,所有像素的大小都会执行格式转换。为避免在实施完全映射复制时代码中断,建议仅在相同格式的曲面之间进行PixelArray到Pixel数组的复制。

New in pygame 1.9.4:

  • 添加了Close()方法。为了明确地清理。

  • 能够使用PixelArray作为清理的上下文管理器。

  • 在不使用引用计数(PyPy)的情况下工作时,这两种方法都很有用。

New in pygame 1.9.2:

  • 数组结构接口

  • 转置方法

  • 长度为1维的广播

Changed in pygame 1.9.2:

  • 二维PixelArray的长度可以为1维。只有2D PixelArray上的整数索引才会返回一维数组。

  • 对于赋值,元组只能是一种颜色。任何其他序列类型都是颜色序列。

surface
Gets the Surface the PixelArray uses.
surface -> Surface

为其创建PixelArray的曲面。

itemsize
Returns the byte size of a pixel array item
itemsize -> int

这与以下内容相同 Surface.get_bytesize() 用于像素阵列的表面。

New in pygame 1.9.2.

ndim
Returns the number of dimensions.
ndim -> int

像素阵列可以是1维或2维的。

New in pygame 1.9.2.

shape
Returns the array size.
shape -> tuple of int's

元组或长度 ndim 给出了每个维度的长度。类似于 Surface.get_size()

New in pygame 1.9.2.

strides
Returns byte offsets for each array dimension.
strides -> tuple of int's

元组或长度 ndim 字节数。当一个步长乘以相应的索引时,它给出了该索引相对于数组开始的偏移量。对于具有反转的数组(具有负阶跃),步幅为负值。

New in pygame 1.9.2.

make_surface()
Creates a new Surface from the current PixelArray.
make_surface() -> Surface

从当前像素数组创建新曲面。根据当前像素数组的不同,大小、像素顺序等将不同于原始的Surface。

# Create a new surface flipped around the vertical axis.
sf = pxarray[:,::-1].make_surface ()

New in pygame 1.8.1.

replace()
Replaces the passed color in the PixelArray with another one.
replace(color, repcolor, distance=0, weights=(0.299, 0.587, 0.114)) -> None

通过将像素更改为传递的替换颜色,将像素替换为PixelArray中传递的颜色。

它使用一个简单的加权欧几里得距离公式来计算颜色之间的距离。距离空间的范围从0.0到1.0,用作颜色检测的阈值。这会导致替换也会将颜色相似但不完全相同的像素考虑在内。

这是一个直接影响PixelArray像素的就地操作。

New in pygame 1.8.1.

extract()
Extracts the passed color from the PixelArray.
extract(color, distance=0, weights=(0.299, 0.587, 0.114)) -> PixelArray

通过将所有匹配像素更改为白色来提取传递的颜色,而将不匹配的像素更改为黑色。这将返回一个新的具有黑/白颜色蒙版的PixelArray。

它使用一个简单的加权欧几里得距离公式来计算颜色之间的距离。距离空间的范围从0.0到1.0,用作颜色检测的阈值。这会导致提取时也会考虑颜色相似但不完全相同的像素。

New in pygame 1.8.1.

compare()
Compares the PixelArray with another one.
compare(array, distance=0, weights=(0.299, 0.587, 0.114)) -> PixelArray

将PixelArray的内容与传入的PixelArray中的内容进行比较。它返回一个带有黑色/白色蒙版的新PixelArray,该蒙版指示两个数组的差异(黑色)。两个PixelArray对象必须具有相同的位深度和尺寸。

它使用一个简单的加权欧几里得距离公式来计算颜色之间的距离。距离空间的范围从0.0到1.0,用作颜色检测的阈值。这会导致比较用类似但不完全相同的颜色将像素标记为白色。

New in pygame 1.8.1.

transpose()
Exchanges the x and y axis.
transpose() -> PixelArray

此方法返回交换了行和列的像素数组的新视图。因此,对于(w,h)大小的数组,返回一个(h,w)切片。如果阵列是一维的,则添加1 x维度的长度,从而产生2D像素阵列。

New in pygame 1.9.2.

close()
Closes the PixelArray, and releases Surface lock.
transpose() -> PixelArray

此方法用于显式关闭PixelArray,并释放表面上的锁。

New in pygame 1.9.4.




Edit on GitHub