使用pygame函数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:Unupupport Array Item Type
。
来自 documentation :
surface_to_array(阵列,surance,kind ='p',opaque = 255,clear = 0) - > surface_to_array函数不从表面对象复制像素 到2D或3D阵列。取决于参数类和目标数组 尺寸,副本可以是原始像素值,RGB,颜色组件 切片或colorkey alpha透明度值。公认的善值 是单个字符代码“ p”,“ r”,“ g”,“ b”,“ a”和“ c”。种类 代码不敏感,因此“ P”等效于“ P”。前两个 目标的尺寸必须是表面尺寸(W,h)。
默认的'p'flind代码执行直接的原始整数像素(映射) 价值副本到2D数组和“ RGB”像素组件(未上限)副本 到具有形状的3D阵列(W,H,3)。对于8位的表面 这意味着表索引复制到2D数组,而不是表 价值本身。 2D阵列的物品大小必须至少与 Surface的像素字节大小。 3D数组的项目大小必须在 至少一个字节
对于'r','g','b'和a'副本是一种单一的颜色组成部分 将未框的表面像素复制到目标2D阵列。为了 与源alpha相称的“ A”和表面(创建了表面 srcalpha flag),有一个colorkey(surance.set.set_colorkey()), 或具有毯子alpha(设置为Surface.set.alpha())然后alpha 值是SDL表面的预期值。如果表面没有 显式alpha值,然后目标阵列充满了值 可选的不透明surface_to_array参数(默认255:不 透明)。
复制'c'是源表面alpha副本的特殊情况 与colorkey。与“ A”颜色组件副本不同,明确的参数 值用于colorkey匹配,否则不透明。默认情况下, 匹配具有alpha 0(完全透明),而其他一切都是 Alpha 255(完全不透明)。这是更一般的实施 pygame.surfarray.array_colorkey()将colorkey值复制到2D 数组。
特定于Surface_to_array,为目标数组提高了一个值 形状或项目大小不正确。为一个 不正确的类型代码。表面特定的问题,例如锁定,增加 pygame.error。
顺便说一句,如果您只能推荐另一种方法来实现相同的目标,那都是好的。
I've been sitting on this for a while now but the documentation is really confusing and has no examples.
I'm trying to store the rgb values of every pixel in a surface. I've tried the following code:
original_pixels = numpy.zeros((wn_width, wn_height, 3))
pygame.pixelcopy.surface_to_array(original_pixels, wn, 'P')
but it throws the error ValueError: Unsupport array item type
.
From the documentation:
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.
By the way if you can just recommend a different approach to achieve the same goal it's all good.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码几乎是正确的,但是默认情况下 /code> 创建一个 floats 的数组。但是,与 /代码> 数据类型必须为 uint8 :
Your code is almost correct, but by default
numpy.zeros
creates an array of floats. However, for use withpygame.pixelcopy.surface_to_array
the datatype must be uint8: