Generalities
NumPy is a package for scientific computing with Python. Smil allows to
- access, with NumPy functions, the Smil image pixels with the method Image::getNumpyArray().
- This is not a copy of the data but a real access to the image pixels via NumPy.
- create a Smil image from a NumPy array with one of the methods Image::fromNumpyArray(arr) or Image(arr) or NumpyInt(arr).
- Note
- These functions are available only inside Python environnment.
Some examples
From Numpy to Smil
- Create a Smil image from a Numpy array with Image()
5ar = np.zeros((32, 32),
'uint8')
- Create a Smil image from a Numpy array with fromNumpyArray()
5a = np.array(range(100),
'uint8')
- Note
- The content of the image is a copy of the content of the Numpy array;
- The image size will be derived from the array shape;
- for a more complex example, take a look on "how to read 3D Tiff images" at smilPyGoodies/smilPyRead3D.
From Smil to Numpy
From a Smil image, do some operation under Numpy. Use getNumpyArray().
13p = img.getNumpyArray()
- Note
- the array returned by getNumpyArray() is a pointer to the Smil image, not a copy.
From Smil to Numpy to Smil
- From a
16 bits
Smil Image, use Numpy to convert it to 8 bits
image
7im16 = sm.Image(
'UINT16')
8sp.readRAW(file, 700, 700, 700, im16)
13p16 = im16.getNumpyArray()
22im8 = sm.Image(im16,
'UINT8')
- Use Numpy to easily create a Smil image with a circle inside it.
12imArr = im1.getNumpyArray()
15print(
"Array dims:", imArr.shape)
20radius, cx, cy = 64, sy//2, sx//2
21y, x = np.ogrid[0:sx, 0:sy]
23index = (x - cx)**2 + (y - cy)**2 <= radius**2
24imArr[:,:][index] = 255