Wednesday, September 23, 2026

Loading/Writing an image with scikit-image

 

 To begin any image processing task in Python, the fundamental first step is to load the image data into your script. This typically involves using libraries like `scikit-image` (`skimage`), `OpenCV` (`cv2`), or `Pillow` (`PIL`) to read image files (e.g., JPEG, PNG, TIFF) into a format that Python can manipulate, often as NumPy arrays.


In this series of post, we'll use the package scikit-image.

1. Loading a sample image of scikit-image

The script always comprise the same code: (i) import the relevant libraries/modules. Here, we have numpy, skimage (scikit-image) and matplotlib.pyplot (for display). (ii) Then, we load a built-in image as a numpy array with the module data of scikit-image

++++ Python Script ++++>++++ End of Python Script ++++

Here is the detailed explanation:

1.1. Importing Libraries
  • * import numpy as np: Imports NumPy, the foundation for handling numerical data and arrays in Python. The alias np is standard practice.
  • * import matplotlib.pyplot as plt: Imports the plotting module from Matplotlib, used to render charts, plots, and images. The alias plt is also standard.
  • * import skimage as ski: Imports scikit-image, a dedicated image processing library built on top of NumPy.
1.2. Loading the Image
  •  img = ski.data.coins(): Loads a built-in grayscale image of several coins provided by scikit-image and saves it into the variable img. Under the hood, img is stored as a NumPy 2D array of numbers representing pixel brightness.
  • To confirm that img is a numpy array (<class 'numpy.ndarray'>), you can add the line print(type(img))
1.3. Displaying the Image
  •  plt.imshow(img, cmap='gray'): Prepares img for display. The cmap='gray' argument tells Matplotlib to render the array using a grayscale color map (0 = black, 255 = white).
  •  plt.show(): Opens a window (or renders in your notebook) to display the prepared image (Fig.1).



Fig. 1: Image coins from skimage

2. Loading an image from a URL

The code is rather the same except we use the function imread(..) from the io module of skimage. Here is the code...

++++ Python Script ++++++++ End of Python Script ++++

3. Saving an image

TODO

4. Reading Information and Pixels

4.1. The Script

++++ Python Script ++++++++ End of Python Script ++++

4.2. Getting image information: shape, dimensions, datatype

In the script above, 

  1. First, we load the color image clown.png (lines #5 and #6)
  2. From lines #8 up to #13, we get some general information about the image shape, the dimension number, and the datatype. These information are crucial to understand the data structure.
    1. shape: an attribute indicating the data structure. For a color image, this typically returns a tuple (height, width, channels).
    2. ndim: the number of dimensions (it is equivalent to len(img.shape) )
    3. dtype: The datatype indicating the dynamic range of the image (8-bit,16-bit, float 32-bit, etc.). Here, uint8 corresponds to 8-bit unsigned integers.

Exercise: Load the images and try to understand their data structure (the meaning of each axis) and try to display them. 

  1. Sample: https://imagej.net/ij/images/baboon.gif ★☆☆
  2. Sample: https://imagej.net/ij/images/apple.tif ★☆☆
  3. Sample: https://imagej.net/ij/images/fluoview-multi.tif ★★☆
  4. Sample: https://imagej.net/ij/images/mitosis.tif ★★★

4.3. Reading pixel values
  1. From lines #14 up to #22, for reading a pixel value,  we use the List indexing syntax. The only difference is the simplified syntax for multiple indexes ( img[y][x] is replaced by img[y,x]). Because, we are reading pixel values of a color RGB image, we get a triplet composed of the red, green, blue components. To read each component separately (from line #18), we need to use the third axis img[y,x,channel].

Thank you for reading.



3. Other crazybiocomputing posts

Further readings are available in ...
  • Image Enhancement Series  [Link]
  • Image Processing TOC [Link]
  • Wikipedia about Kernel [Link]







No comments:

Post a Comment