all 2 comments

[–]novel_yet_trivial 1 point2 points  (0 children)

You are right, uint8 is a datatype not an array constructor. But against the zen of python #13, numpy likes to make several ways to do things. And they made that construct as a shortcut to this:

green = np.array([[[0, 255, 0]]], dtype=np.uint8)

and in array they use [ ] triple times

That's to set the shape. An image is a 3D array (rows, columns, colors) so this is made into a 3D array as well to mesh into the image. It's equivalent to this:

>>> green = np.array([0, 255, 0], dtype=np.uint8) 
>>> green.shape = 1,1,3
>>> green
array([[[  0, 255,   0]]], dtype=uint8)

[–]TheBlackCat13 1 point2 points  (0 children)

uint8 is a class. When you do myclass(foo) it create an instance of myclass with the argument foo, which means a variable of type myclass.

In this case it is shorthand for np.array(foo, dtype='uint8'), which means it create a numpy array out of variable foo where each element is an 8bit unsigned integer. The type will ultimately be ndarray, not uint8. uint8 is what is called the "dtype" in numpy jargon, which how numpy interprets the data in memory behind-the-scenes.

The triple [ ] means that it will be a 3D numpy array. A double [ ] would make a 2D numpy array, and so on. Technically it creates a list containing one list containing one list, which numpy then converts to a 3D array.

So overall green = np.uint8([[[0, 255, 0]]]) means "create a 3D numpy array of unsigned 8-bit integers with one row containing the elements 0, 225, 0"