Scikit Image - Using Plugins



A plugin refers to an extension or external software component that can enhance the functionality of the program by adding specific features to it thus improving its functionality.

Plugins in Python Scikit Image

The Python scikit-image (skimage) library comes with a variety of plugins that can be used to handle image IO operations, such as reading, writing, and displaying images. The available plugins in the scikit-image library include popular libraries like Matplotlib, PIL (Python Imaging Library), GDAL, SimpleITK, tifffile, PyFITS, and ImageIO. Each plugin specializes in a specific image IO operation.

The scikit-image library loads the plugins as needed, to ensure optimal performance, and to allow efficient resource utilization. This means that plugins are only loaded when explicitly required or when set as default. This dynamic loading mechanism ensures that only the necessary plugins are loaded, depending on the specific image I/O operation.

Also, the scikit-image library provides a range of functional tools to handle and operate plugins. These tools allow users to customize their image IO operations, let’s discuss some key plugin functions available in the skimage.io module.

Listing the available plugins

The function io.find_available_plugins(loaded=False) is used to list the available plugins in scikit-image (skimage.io). And it returns a dictionary with plugin names as keys and exposed functions as values. Following is the syntax of this function −

skimage.io.find_available_plugins(loaded=False)

The parameter "loaded" takes a boolean value. If it is set to True, only the loaded plugins will be shown. By default, all plugins are displayed.

Example 1

The following example demonstrates the use of io.find_available_plugins() function to list all the available plugins and their corresponding exposed functions.

import skimage.io as io
# List all available plugins
available_plugins = io.find_available_plugins()
# Display the plugin names and their exposed functions
for plugin_name, exposed_functions in available_plugins.items():
   print('Plugin:', plugin_name)
   print("Exposed Functions:", exposed_functions)
   print()

Output

Plugin: fits
Exposed Functions: ['imread', 'imread_collection']
Plugin: gdal
Exposed Functions: ['imread', 'imread_collection']
Plugin: gtk
Exposed Functions: ['imshow']
Plugin: imageio
Exposed Functions: ['imread', 'imsave', 'imread_collection']
Plugin: imread
Exposed Functions: ['imread', 'imsave', 'imread_collection']
Plugin: matplotlib
Exposed Functions: ['imshow', 'imread', 'imshow_collection',
'imread_collection']
Plugin: pil
Exposed Functions: ['imread', 'imsave', 'imread_collection']
Plugin: qt
Exposed Functions: ['imshow', 'imsave', 'imread', 'imread_collection']
Plugin: simpleitk
Exposed Functions: ['imread', 'imsave', 'imread_collection']
Plugin: tifffile
Exposed Functions: ['imread', 'imsave', 'imread_collection']

Example 2

Let's get the information about loaded plugins only.

from skimage import io
# List loaded plugins only
available_plugins = io.find_available_plugins(loaded=True)
# Display the loaded plugin names and their exposed functions
for plugin_name, exposed_functions in available_plugins.items():
   print('Plugin:', plugin_name)
   print("Exposed Functions:", exposed_functions)
   print()

Output

Plugin: imageio
Exposed Functions: ['imread', 'imsave', 'imread_collection']
Plugin: matplotlib
Exposed Functions: ['imshow', 'imread', 'imshow_collection',
'imread_collection']
Plugin: tifffile
Exposed Functions: ['imread', 'imsave', 'imread_collection']

Retrieving Info about a specific plugin

The function io.plugin_info(plugin) is used to retrieve information about a specific plugin. It returns a dictionary containing information about the plugin, such as the description, and provides nothing but available function names. Following is the syntax of this function −

skimage.io.plugin_info(plugin)

The parameter "plugin" takes a string representing the name of the plugin for which you want to retrieve information.

Example 1

The following example demonstrates how to use the io.plugin_info() function to retrieve the information about the 'pil' plugin.

from skimage import io
# Get information about the 'pil' plugin
plugin_info = io.plugin_info('pil')
# Print the plugin information
print("Description:", plugin_info['description'])
print("Available Functions:", plugin_info['provides'])
print()

Output

Description: Image reading via the Python Imaging Library
Available Functions: imread, imsave

Example 2

In this example, we will get information about the 'matplotlib' plugin.

from skimage import io
# Get information about the 'matplotlib' plugin
plugin_info = io.plugin_info('matplotlib')
# Print the plugin information
print("Description:", plugin_info['description'])
print("Available Functions:", plugin_info['provides'])
print()

Output

Description: Display or save images using Matplotlib
Available Functions: imshow, imread, imshow_collection, _app_show

Retrieving the plugin order

The io.plugin_order() function is used to obtain the currently preferred plugin order. Following is the syntax of this function −

skimage.io.plugin_order()

The function returns a dictionary with function names as keys and the corresponding values are lists of plugins in the order of preference.

Example

Following is an example of using the plugin_order() function to obtain the currently preferred plugin order.

from skimage import io
# Get the currently preferred plugin order
order_dict = io.plugin_order()
# Print the plugin loading order
print("Preferred Plugin Order:")
for function_name, plugins in order_dict.items():
   print("Function:", function_name)
   print("Plugins in order of preference:", plugins)
   print()

Output

Preferred Plugin Order:
Function: imread
Plugins in order of preference: ['imageio', 'matplotlib']
Function: imsave
Plugins in order of preference: ['imageio']
Function: imshow
Plugins in order of preference: ['matplotlib']
Function: imread_collection
Plugins in order of preference: ['imageio', 'matplotlib']
Function: imshow_collection
Plugins in order of preference: ['matplotlib']
Function: _app_show
Plugins in order of preference: ['matplotlib']

Setting the default plugin

The io.use_plugin() function is used to set the default plugin for a specified operation.

The specified plugin will be loaded if it hasn't been loaded already. Following is the syntax of this function −

skimage.io.use_plugin(name, kind=None)

Here are the parameters of this function −

  • name − A string representing the name of the plugin is to be set as the default.
  • kind (optional) − A string represents the specific function for which the plugin is being set. It can take one of the following values: 'imsave', 'imread', 'imshow', 'imread_collection', 'imshow_collection'. By default, the plugin is set for all functions.

Example

The following example shows the change in the plugin loading order after setting the default plugin.

from skimage import io

# Get the currently preferred plugin order 
order_dict_1 = io.plugin_order()

# Print the plugin loading order
print("Plugin Order before Setting the default plugin:")
for function_name, plugins in order_dict_1.items():
    print(function_name)
    print("Plugin order:", plugins)
    
    print()

# Set the default plugin for all functions
io.use_plugin('qt')

# Get the preferred plugin order after setting the default plugin
order_dict_2 = io.plugin_order()

# Print the plugin loading order
print("Plugin Order after setting the default plugin: ")
for function_name, plugins in order_dict_2.items():
    print(function_name)
	print("Plugin order:", plugins)
    
    print()

Output

Plugin Order before Setting the default plugin:
imread
Plugin order: ['imageio', 'matplotlib']

imsave
Plugin order: ['imageio']

imshow
Plugin order: ['matplotlib']

imread_collection
Plugin order: ['imageio', 'matplotlib']

imshow_collection
Plugin order: ['matplotlib']

_app_show
Plugin order: ['matplotlib']

Plugin Order after setting the default plugin: 
imread
Plugin order: ['qt', 'imageio', 'matplotlib']

imsave
Plugin order: ['qt', 'imageio']

imshow
Plugin order: ['qt', 'matplotlib']

imread_collection
Plugin order: ['qt', 'imageio', 'matplotlib']
imshow_collection
Plugin order: ['matplotlib']

_app_show
Plugin order: ['qt', 'matplotlib']

Resetting the plugin to its default state

The function io.reset_plugins() is used to reset the plugin state to its default/initial state. i.e., where no plugins are loaded. Following is the syntax of this function −

skimage.io.reset_plugins()

Example

The following example demonstrates the use of io.reset_plugins() function to reset the plugin state to its default state. And it retrieves the plugin order before and after resetting the plugin state to the default.

from skimage import io

# Get the currently preferred plugin order 
order_dict_1 = io.plugin_order()

# Print the plugin loading order
print("Plugin Order before Resetting to the default state:")
for function_name, plugins in order_dict_1.items():
   if function_name == 'imread':
      print('Function',function_name)
      print("Plugin order:", plugins)

      print()

# Reset the plugin state to the default
io.reset_plugins()

# Get the preferred plugin order after Resetting to the default state
order_dict_2 = io.plugin_order()
# Print the plugin loading order
print("Plugin Order after Resetting to the default state: ")
for function_name, plugins in order_dict_2.items():
   if function_name == 'imread':
      print('Function',function_name)
      print("Plugin order:", plugins)
      print()

Output

Plugin Order before Resetting to the default state:
Function imread
Plugin order: ['pil', 'imageio', 'matplotlib']

Plugin Order after Resetting to the default state: 
Function imread
Plugin order: ['imageio', 'matplotlib']

Appropriate plugin for a specific function

The io.call_plugin() function is used to find the appropriate plugin for a specified function and execute it. Following is the syntax of this function −

skimage.io.call_plugin(kind, *args, **kwargs)

Here are the parameters of this function −

  • kind (str) − The function to look up. It can take one of the following values: 'imshow', 'imsave', 'imread', 'imread_collection'.
  • plugin (str, optional) − The specific plugin to load. If not provided (default is None), the first matching plugin will be used.
  • *args, **kwargs − Additional arguments and keyword arguments that will be passed to the plugin function.

Example

The following example demonstrates the use of io.call_plugin() function to load an image file using the appropriate plugin for the image reading function.

import numpy as np
from skimage import io

img_array = io.call_plugin('imread', 'Images_/black rose.jpg')
print("Image Arary Shape:",img_array.shape)

Output

Image Arary Shape: (2848, 4272, 3)

The function loads the image file and returns it as a NumPy array, which is assigned to the img_array variable.

Advertisements