large size numpy array 8bit to 16bit
To convert a large size numpy array from 8-bit to 16-bit, you can use the numpy function astype(). This method can convert the data type of a numpy array to a specified data type.
Here's an example:
import numpy as np
# create an 8-bit array
arr8 = np.random.randint(0, 256, size=(1000, 1000)).astype(np.uint8)
# convert to 16-bit array
arr16 = arr8.astype(np.uint16)
# check the data types
print(arr8.dtype)
print(arr16.dtype)
In this example, we first create an 8-bit numpy array with random integer values between 0 and 255. We then use the astype() method to convert this array to a 16-bit numpy array by specifying the data type as np.uint16. Finally, we print the data types of both arrays to confirm that the conversion was successful.
Note that the conversion from 8-bit to 16-bit will result in an increase in memory usage, as each element in the array now takes up twice as much space.
原文地址: http://www.cveoy.top/t/topic/btKN 著作权归作者所有。请勿转载和采集!