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

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