I'm following this tutorial on how to use numpy to manipulate images. When I load the sample image using scipy, I get a 2D array of RGB tuples, with a dtype value appended on the end.
array([[7, 8, 5],
[3, 5, 7]], dtype=uint8)
def myfunc(a, b):
return a + 2
vfunc = np.vectorize(myfunc)
array([[9, 10, 7],
[5, 7, 9]])
dtype=uint8 is not an element of the array. It is just a thing that gets printed to let you know that the array is of type
The default types
np.int_ do not get a printout like that, which is what you are seeing in the second case. The way you can tell float and int arrays apart is that float arrays will always have decimal points in the numbers.
The reason that this is happening is that you are adding
2 to each element of your array. Since
2 is an integer, the output array gets promoted to
np.int_ type and you do not get an explicit
You can try the following experiment: redefine
myfunc to add a
np.uint8 instead of an integer to the array elements and try to print the result:
def myfunc(a, b): return a + np.uint8(2)
Finally, keep in mind that vectorizing Python code is usually not the best way to get things done. The function itself will be a Python function, and therefore slow. It is generally better to find a way of performing whatever operations you want with numpy functions.