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::getNumArray().
- 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::fromNumArray(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()
1 import smilPython
as sp
5 ar = np.zeros((32, 32),
'uint8')
- Create a Smil image from a Numpy array with fromNumArray()
1 import smilPython
as sp
5 a = 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 getNumArray().
2 import smilPython
as sm
- Note
- the array returned by getNumArray() 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
2 import smilPython
as sm
7 im16 = sm.Image(
'UINT16')
8 sp.readRAW(file, 700, 700, 700, im16)
13 p16 = im16.getNumArray()
19 p8 = p.astype(
'uint8')
22 im8 = sm.Image(im16,
'UINT8')
- Use Numpy to easily create a Smil image with a circle inside it.
2 import smilPython
as sp
12 imArr = im1.getNumArray()
15 print(
"Array dims:", imArr.shape)
20 radius, cx, cy = 64, sy//2, sx//2
21 y, x = np.ogrid[0:sx, 0:sy]
23 index = (x - cx)**2 + (y - cy)**2 <= radius**2
24 imArr[:,:][index] = 255