使用 pygame 函数 pygame.pixelcopy.surface_to_array()
Using the pygame function pygame.pixelcopy.surface_to_array()
我已经研究了一段时间了,但是文档真的很混乱而且没有示例。
我正在尝试存储表面中每个像素的 rgb 值。我尝试了以下代码:
original_pixels = numpy.zeros((wn_width, wn_height, 3))
pygame.pixelcopy.surface_to_array(original_pixels, wn, 'P')
但它抛出错误 ValueError: Unsupport array item type
。
surface_to_array(array, surface, kind='P', opaque=255, clear=0) ->
None The surface_to_array function copies pixels from a Surface object
to a 2D or 3D array. Depending on argument kind and the target array
dimension, a copy may be raw pixel value, RGB, a color component
slice, or colorkey alpha transparency value. Recognized kind values
are the single character codes 'P', 'R', 'G', 'B', 'A', and 'C'. Kind
codes are case insensitive, so 'p' is equivalent to 'P'. The first two
dimensions of the target must be the surface size (w, h).
The default 'P' kind code does a direct raw integer pixel (mapped)
value copy to a 2D array and a 'RGB' pixel component (unmapped) copy
to a 3D array having shape (w, h, 3). For an 8 bit colormap surface
this means the table index is copied to a 2D array, not the table
value itself. A 2D array's item size must be at least as large as the
surface's pixel byte size. The item size of a 3D array must be at
least one byte.
For the 'R', 'G', 'B', and 'A' copy kinds a single color component of
the unmapped surface pixels are copied to the target 2D array. For
kind 'A' and surfaces with source alpha (the surface was created with
the SRCALPHA flag), has a colorkey (set with Surface.set_colorkey()),
or has a blanket alpha (set with Surface.set_alpha()) then the alpha
values are those expected for a SDL surface. If a surface has no
explicit alpha value, then the target array is filled with the value
of the optional opaque surface_to_array argument (default 255: not
transparent).
Copy kind 'C' is a special case for alpha copy of a source surface
with colorkey. Unlike the 'A' color component copy, the clear argument
value is used for colorkey matches, opaque otherwise. By default, a
match has alpha 0 (totally transparent), while everything else is
alpha 255 (totally opaque). It is a more general implementation of
pygame.surfarray.array_colorkey()Copy the colorkey values into a 2d
array.
Specific to surface_to_array, a ValueError is raised for target arrays
with incorrect shape or item size. A TypeError is raised for an
incorrect kind code. Surface specific problems, such as locking, raise
a pygame.error.
顺便说一句,如果您能推荐一种不同的方法来实现相同的目标,那就太好了。
您的代码几乎是正确的,但默认情况下 numpy.zeros
creates an array of floats. However, for use with pygame.pixelcopy.surface_to_array
数据类型必须是 uint8:
original_pixels = numpy.zeros((wn_width, wn_height, 3), dtype=numpy.uint8)
pygame.pixelcopy.surface_to_array(original_pixels, wn, 'P')
我已经研究了一段时间了,但是文档真的很混乱而且没有示例。
我正在尝试存储表面中每个像素的 rgb 值。我尝试了以下代码:
original_pixels = numpy.zeros((wn_width, wn_height, 3))
pygame.pixelcopy.surface_to_array(original_pixels, wn, 'P')
但它抛出错误 ValueError: Unsupport array item type
。
surface_to_array(array, surface, kind='P', opaque=255, clear=0) -> None The surface_to_array function copies pixels from a Surface object to a 2D or 3D array. Depending on argument kind and the target array dimension, a copy may be raw pixel value, RGB, a color component slice, or colorkey alpha transparency value. Recognized kind values are the single character codes 'P', 'R', 'G', 'B', 'A', and 'C'. Kind codes are case insensitive, so 'p' is equivalent to 'P'. The first two dimensions of the target must be the surface size (w, h).
The default 'P' kind code does a direct raw integer pixel (mapped) value copy to a 2D array and a 'RGB' pixel component (unmapped) copy to a 3D array having shape (w, h, 3). For an 8 bit colormap surface this means the table index is copied to a 2D array, not the table value itself. A 2D array's item size must be at least as large as the surface's pixel byte size. The item size of a 3D array must be at least one byte.
For the 'R', 'G', 'B', and 'A' copy kinds a single color component of the unmapped surface pixels are copied to the target 2D array. For kind 'A' and surfaces with source alpha (the surface was created with the SRCALPHA flag), has a colorkey (set with Surface.set_colorkey()), or has a blanket alpha (set with Surface.set_alpha()) then the alpha values are those expected for a SDL surface. If a surface has no explicit alpha value, then the target array is filled with the value of the optional opaque surface_to_array argument (default 255: not transparent).
Copy kind 'C' is a special case for alpha copy of a source surface with colorkey. Unlike the 'A' color component copy, the clear argument value is used for colorkey matches, opaque otherwise. By default, a match has alpha 0 (totally transparent), while everything else is alpha 255 (totally opaque). It is a more general implementation of pygame.surfarray.array_colorkey()Copy the colorkey values into a 2d array.
Specific to surface_to_array, a ValueError is raised for target arrays with incorrect shape or item size. A TypeError is raised for an incorrect kind code. Surface specific problems, such as locking, raise a pygame.error.
顺便说一句,如果您能推荐一种不同的方法来实现相同的目标,那就太好了。
您的代码几乎是正确的,但默认情况下 numpy.zeros
creates an array of floats. However, for use with pygame.pixelcopy.surface_to_array
数据类型必须是 uint8:
original_pixels = numpy.zeros((wn_width, wn_height, 3), dtype=numpy.uint8)
pygame.pixelcopy.surface_to_array(original_pixels, wn, 'P')