On Wed, Apr 8, 2009 at 20:39, Ian Mallett <geometr...@gmail.com> wrote: > Hello, > > I want to make an array of size sqrt(n) by sqrt(n) by 3, filled with special > values. > > The values range from 0.0 to 3.0, starting with 0.0 at one corner and ending > at 3.0 in the opposite, increasing going row by row. The value is to be > encoded in each color. Because this is somewhat abstract, here's a small > example (n=25), generated using the attached code (it also multiplies the > number by 255 to obtain a RGB color and not messy floats) to show the > concept. The real version should be done by NumPy. This is where I need > help; I have no idea how to even approach the problem. > > [[ 0, 0, 0],[ 32, 0, 0],[ 64, 0, 0],[ 96, 0, 0],[128, 0, 0], > [159, 0, 0],[191, 0, 0],[223, 0, 0],[255, 0, 0],[255, 32, 0], > [255, 64, 0],[255, 96, 0],[255,128, 0],[255,159, 0],[255,191, 0], > [255,223, 0],[255,255, 0],[255,255, 32],[255,255, 64],[255,255, 96], > [255,255,128],[255,255,159],[255,255,191],[255,255,223],[255,255,255]] > > Arrays like this need to be generated quite quickly, so the per-pixel method > I presented will not work. How should I do it with NumPy?
In [1]: from numpy import * In [2]: sqrtn = 5 In [3]: n = sqrtn**2 In [4]: x = linspace(0.0, 3.0, n) In [5]: y = column_stack([x, x-1, x-2]).clip(0, 1).reshape([sqrtn, sqrtn, 3]) In [6]: z = (y * 255).round().astype(uint8) In [7]: z Out[7]: array([[[ 0, 0, 0], [ 32, 0, 0], [ 64, 0, 0], [ 96, 0, 0], [128, 0, 0]], [[159, 0, 0], [191, 0, 0], [223, 0, 0], [255, 0, 0], [255, 32, 0]], [[255, 64, 0], [255, 96, 0], [255, 128, 0], [255, 159, 0], [255, 191, 0]], [[255, 223, 0], [255, 255, 0], [255, 255, 32], [255, 255, 64], [255, 255, 96]], [[255, 255, 128], [255, 255, 159], [255, 255, 191], [255, 255, 223], [255, 255, 255]]], dtype=uint8) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion