SMIL 1.1.1
Loading...
Searching...
No Matches
HowTo and Tips & Tricks

smilPython module

Importing

  • How to import smilPython module - there are two options :
         import smilPython as sp
    
         from smilPython import *
    
    Which option privilegiate ? Both have pros and cons :
    • with the first one you need to prefix all calls to smilPython by the prefix sp (or other prefered prefix). The advantage is that this desambiguate all possible function name conflicts (e.g. read()).
    • the second one is simpler, with the possible inconvenients pointed out by the another option.
  • Importing smilPython error - if you get this error, or something similar, most of the time, the reason is one of these :
         In [61]: import smilPython
         ----------------------------------------------------------
         ModuleNotFoundError      Traceback (most recent call last)
         <ipython-input-61-ade08580f09e> in <module>
         ----> 1 import smilPython
    
         ModuleNotFoundError: No module named 'smilPython'
    
    • smilPython isn't installed. Install smilPython;
    • Python or iPython didn't found smilPython path. This is usually solved with something of the kind :
             export PYTHONPATH=/whatever/lib/Smil
      
    • smilPython was compiled for a different version of Python. When building smilPython, it's linked to the installed version of Python. E.g., smilPython built with Python 3.5 won't run with Python 3.6. Recompile or reinstall Smil.

Tips

Displaying intermediate results

If you want to display intermediate results of your processing you need to call any function which will handle the Gui interface events and refresh all images displayed by the application you're working on. Basically you have two kind of options :

  • with pause waiting an order to continue : in this case, processing will stop until the user hits the Enter key :
    1import smilPython as sp
    2
    3# define which structuring element to use
    4se = sp.SquSE()
    5
    6# get an image
    7imIn = sp.Image("https://smil.cmm.minesparis.psl.eu/images/lena.png")
    8# declare output image
    9imOut = sp.Image(imIn)
    10
    11# Display input, temporary and output images
    12imIn.show("Input image")
    13imOut.show()
    14
    15input("Hit the enter key to continue")
    16
    17for i in range(0,8):
    18 s = "Open SE({:})".format(i)
    19 print(s)
    20 imOut.setName(s)
    21
    22 r = sp.open(imIn, imOut, se(i))
    23 # save temporari result, if wanted
    24 r = sp.write(imOut, 'res-tmp-{:03d}'.format(i))
    25 input("Hit the enter key to continue")
    26
    27input("Hit the enter key to exit")
    28
  • with timed pause before continue. The call to Gui.processEvents() will do the job.
    1import smilPython as sp
    2import time
    3
    4# define which structuring element to use
    5se = sp.SquSE()
    6
    7# get an image
    8imIn = sp.Image("https://smil.cmm.minesparis.psl.eu/images/lena.png")
    9# declare output image
    10imOut = sp.Image(imIn)
    11
    12# Display input, temporary and output images
    13imIn.show("Input image")
    14imOut.show()
    15
    16input("Hit the enter key to begin")
    17for i in range(0,8):
    18 s = "Open SE({:})".format(i)
    19 print(s)
    20 imOut.setName(s)
    21
    22 r = sp.open(imIn, imOut, se(i))
    23
    24 # process GUI events and update the image display
    25 sp.Gui.processEvents()
    26 # an optional wait of 0.4 seconds
    27 time.sleep(0.4)
    28
    29input("Hit the enter key to exit")
    30

Functions

  • Order of parameters - the general rule is :

    • input parameters first;
    • output parameters after;
    • optional parameters (including structuring elements) at the end.

    But there are some exceptions. In doubt, check the function prototype.

  • Images as output parameters - shall allways be predefined. Their attributes will be adjusted by the called function.
         imOut = sp.Image()
         r = sp.erode(imIn, imOut)
    
    There are some exceptions for attributes adjustements. For example, when you want to force the data type of the output image. One example is the labelling (label()) of an 'UINT8' input image with more than 256 connected components. In this case the data type of the output image shall be 'UINT16' or 'UINT32'
         imOut = sp.Image(imIn, 'UINT16')
         r = sp.label(imIn, imOut)
    

Other Packages

smilPyGoodies

Some Goodies for smilPython

Download : smilPyGoodies

     git clone https://github.com/MinesParis-MorphoMath/smilPyGoodies