So far I’ve come up with following:
screen_array = pygame.surfarray.pixels2d(self.screen)
noise_small = numpy.random.random((self.width/4,self.height/4)) * 0.2 + 0.4
noise_big = noise_small .repeat(4, 0).repeat(4, 1)
screen_array *= noise_big
But this adds separate noise on every channel.
I know I could also use pygame.surfarray.pixels3d and then numpy.tile the noise array, but that’s way too slow for me (well, what I’m doing now is also pretty slow, but nevermind that).
Each element in the array returned by
pygame.surfarray.pixels2dis a number obtained by packing the 3 channels into a single integer, and I don’t know of a simple method to take advantage of the array structure to do a fast and simple multiplication with this.However, I still think your best bet is to use
pygame.surfarray.pixels3d, but not to usenumpy.tile. You could do something like this :Notice the
[:, :, numpy.newaxis]. This reshape thenoise_bigto be of shape(X, Y, 1)instead of (X, Y), which allows direct multiplication with thescreen_arrayobject, having shape(X, Y, 3). When that happens, the single element of the third axis is multiplied with all 3 channels. This way of tackling the problem is more efficient than usingnumpy.tile.