def array_colorkey (surface):
"""pygame.numpyarray.array_colorkey (Surface): return array
copy the colorkey values into a 2d array
Create a new array with the colorkey transparency value from each
pixel. If the pixel matches the colorkey it will be fully
tranparent; otherwise it will be fully opaque.
This will work on any type of Surface format. If the image has no
colorkey a solid opaque array will be returned.
This function will temporarily lock the Surface as pixels are
copied.
"""
colorkey = surface.get_colorkey ()
if colorkey == None:
# No colorkey, return a solid opaque array.
array = numpy.empty (surface.get_width () * surface.get_height (),
numpy.uint8)
array.fill (0xff)
array.shape = surface.get_width (), surface.get_height ()
return array
# Taken from from Alex Holkner's pygame-ctypes package. Thanks a
# lot.
array = array2d (surface)
# Check each pixel value for the colorkey and mark it as opaque or
# transparent as needed.
val = surface.map_rgb (colorkey)
array = numpy.choose (numpy.equal (array, val),
(numpy.uint8 (0xff), numpy.uint8 (0)))
array.shape = surface.get_width (), surface.get_height ()
return array
评论列表
文章目录