SMIL  1.0.0-dev
Smil <-> NumPy interface

# 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
2 import numpy as np
3
4 # create a 10x10 NumPy array
5 ar = np.zeros((32, 32), 'uint8')
6 ar[8,:] = 255
7 ar[:, 16] = 255
8
9 # creates a Smil image and set it's content to "ar"
10 img = sp.Image(ar)
• Create a Smil image from a Numpy array with fromNumArray()
1 import smilPython as sp
2 import numpy as np
3
4 # create a 10x10 NumPy array
5 a = np.array(range(100), 'uint8')
6 a = a.reshape(10, 10)
7
8 # creates a Smil image and set it's content to "a"
9 img = sp.Image()
10 img.fromNumArray(a)
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().

1
2 import smilPython as sm
3 import numpy as np
4
5 # read a PNG image file (8 bits gray image)
6 file = "my-image.png")
7 img = sm.Image(file)
8
9 # show the image
10 img.show(file)
11
12 # get a NumPy array
13 p = img.getNumArray()
14
15 # let's threshold the image
16 t = 127
17 p[p >= t] = 255
18 p[p < t] = 0
19
20 # Call the "modified" method in order to update the viewer content
21 img.modified()
22
Note
• the array returned by getNumArray() is a pointer to the Smil image, not a copy.

## From Smil to Numpy to Smil

1. From a `16 bits` Smil Image, use Numpy to convert it to `8 bits` image
1
2 import smilPython as sm
3 import numpy as np
4
5 # read a 16 bits RAW Image
6 file = "Slices-16.raw"
7 im16 = sm.Image('UINT16')
8 sp.readRAW(file, 700, 700, 700, im16)
9
10 # Let's convert 8 bit input image
11
12 # get a pointer to a numpy array
13 p16 = im16.getNumArray()
14
15 # scale pixel values from 2**16 to 2**8
16 p16 //= 256
17
18 # get a new 8 bit numpy array
19 p8 = p.astype('uint8')
20
21 # create a 8 bits image with the same dimensions of the 16 bit image
22 im8 = sm.Image(im16, 'UINT8')
23 # come back to Smil Image
24 im8.fromNumArray(p8)
25
2. Use Numpy to easily create a Smil image with a circle inside it.
1
2 import smilPython as sp
3 import numpy as np
4
5 # Create an image
6 sx = 256
7 sy = 384
8 im1 = sp.Image(sx, sy)
9 im1.show()
10
11 # Create a numpy array containing the real image pixels
12 imArr = im1.getNumArray()
13
14 # Display the dimensions of the created array
15 print("Array dims:", imArr.shape)
16
17 # Do something with the array... E.g., draw a circle
18 imArr[:] = 0
19 # the circle will be centered at the center of the image
20 radius, cx, cy = 64, sy//2, sx//2
21 y, x = np.ogrid[0:sx, 0:sy]
22 # get the indexes of the pixels inside the circle
23 index = (x - cx)**2 + (y - cy)**2 <= radius**2
24 imArr[:,:][index] = 255
25
26 # Call the "modified" method in order to update the viewer content
27 im1.modified()
28
29 input()
30