Marius Gedminas wrote:
On Fri, Feb 27, 2009 at 11:16:28AM +1100, René Dudfield wrote:
hey,

is it possible to use numpy instead of Numeric?  Numeric really is
dying now...  even we are going to stop trying to keep it working.

I suppose I should.  Since I'm really clueless about
Numeric/numarray/numpy, please tell me if this code has any obvious
shortcomings:

  # initialization, done once
  import pygame
  import numpy
  image = pygame.image.load('title.png')   # has an alpha channel
  mask = pygame.surfarray.array_alpha(image)

  # this is done once every frame
  array = pygame.surfarray.pixels_alpha(image)
  alpha = 42.5 # a float varying between 1 and 255
  array[...] = (mask * alpha / 255).astype('b')

Cheers!
Marius Gedminas
Well, alpha and 255 are both scalars, so (mask * (alpha / 255)) saves one intermediate array. Also the preferred NumPy convention is to use dtypes (data-types) rather than type characters: .astype(numpy.uint8). But arithmetic operations have ufunc equivalents which take an optional output array. This means the astype(), along with its intermediate array, can be removed. It probably also means the intermediate array float array goes away as well. So it is likely this alternative uses no intermediate arrays.

## array = pygame.surfarray.pixels_alpha(image)
alpha = 42.5
## array[...] = (mask * alpha / 255).astype('b')
numpy.multiply(mask, alpha  /  255, pygame.surfarray.pixels_alpha(image))


Lenard

--
Lenard Lindstrom
<le...@telus.net>

Reply via email to