matplotlib tutorial (7) nitta@tsuda.ac.jp

Chapter 7: Saving / loading / Displaying of Images

"Save to image file".

For the sake of simplicity, this tutorial will use the image data from the database of tensorflow.keras.

Google Colab can use tensorflow by default, but if tensorflow is not installed in your jupyter notebook environment, install it whth the pip command.

Rule [7-1]: When displaying image data of Numpy array format, convert grayscale images to uint8 type arrays of [0, 255] and color images to float32 type arrays of [0, 1], and then apply Axes.imshow().

For a gray scale image (number of channel = 1), convert it to a Numpy array with 'unit8' type elements of range [0, 255]. And for a color image (number of channels = 3 or 4), convert it to a Numpy array with 'float32' type element of range [0.0, 1.0].

Then apply the 'Axes.imshow()' function to the image.

7-1: Display a grayscale image, using Axes.imshow()

The image displayed must be a 2-dimensional array. If the elements of the array are integers, the brightness (0: black, 255: white) is expressed in range [0, 255]. When the element of the array are 'float', the brightness (0.0: black, 1.0: white) is expressed in the range [0.0, 1.0].

7-2: Display color images, using Axes.imshow()

Color image data is a tensor in (Rows, Cols, Channels) or (Channels, Rows, Cols) format. The number of channels element is 3 which represents the brightness of RGB.

In CIFAR10 using now, the image format is (Rows, Cols, Channels).

When each element of RGB is an integer, it represents brightness (0: dark, 255: bright) in the range [0, 255]. When each element of RGB is a floating fraction, the brightness (0.0: dark, 1.0: bright) is expressed in the range [0.0, 1.0].

Preparation for 7-3: Download and extract image files from the network

In preparation for 7-3, download (a part of) the face image file of VidTIMIT dataset from the network and extract it.

Official WWW of VidTIMIT dataset:
http://conradsanderson.id.au/vidtimit/

zip files of 2 persons of VidTIMIT dataset:
https://zenodo.org/record/158963/files/fadg0.zip
https://zenodo.org/record/158963/files/faks0.zip

7-3: Load images from files

It is asumed that the image file is in the following path.

./data/fadg0/video/head/[0-9]*

Rule [7-2]: Use the load_img() function of 'tensorflow.keras' to load image data from an image file.

load_img() / img_to_array() / array_to_img() / save_img() function is in either

depending on the version of tensorflow.

Since the return value of the load_img() function is PIL format image data, it is easier to use later if it is converted to a Numpy array.

    image_pil = load_img(path)

Rule [7-3]: The image data returned from load_img() function might be immediately converted from PIL format to Numpy array.

load_img() / img_to_array() / array_to_img() / save_img() function is in either

depending on the version of tensorflow. Since the return value of the load_img() function is PIL format image data, it is easier to use later if it is converted to a Numpy array. You can use the img_to_array() function for this conversion, but the numpy.array() function seems to be more popular.

The img_to_array() function returns a Numpy array whose element is 'uint8' of range [0, 255]. Applying np.array() to PIL format data without specifying dtype parameter also returns a Numpy array whose element is 'uint8' of range [0, 255].

    image_uint8 = np.array(imgage_pil)

Rule [7-4]:Convert image data to the Numpy array with 'float32' element type of the range [0.0, 1.0] or [-1.0, 1.0].

When passing image data through a neural network, convert it to the Numpy array with 'float32' element type of the range [0.0, 1.0] or [-1.0, 1.0].

Use the following code to convert from a Numpy array of 'uint8' element type of range [0, 255].

  image = image_uint8.astype('float32') / 255.   # [0, 255] --> [0, 1]
or
  image = image_uint8.astype('float32') / 127.5 - 127.5  # [0, 255] --> [-1, 1]

The code of the reverse conversion is as follows.

    image_uint8 = (image * 255).astype('uint8')   # [0, 1] ---> [0, 255]
or
    image_uint8 = ((image + 1) * 127.5).astype('uint8') # [-1, 1] --> [0, 255]

Rule [7-5]:When displaying or saving a color image data, convert it to the Numpy array of 'float32' element with range[0.0, 1.0].

When displaying or saving a color image data, convert it to the Numpy array of 'float32' element with range[0.0, 1.0]. Use the following code to convert a Numpy array of 'float32' element type of the range [-1.0, 1.0] to the range [0.0, 1.0].

To convert between a Numpy array with 'float32' element type of range [0.0, 1.0] and that of range [-1.0, 1.0].

    image = image * 2 - 1      # [0, 1] ---> [-1, 1]
    image = (image + 1) / 2    # [-1, 1] --> [0, 1]

Use the numpy.clip() function to guarantee the range of element values.

    image = np.clip(image, 0, 1)    # clipping element values between 0 and 1.

7-4: Conversion of the range of element values of image data

Rule [7-6]: Usae the save_img() function to save image data in Numpy array to a file.

The function of load_img() / img_to_array() / array_to_img() / save_img() is in either

depending the version of tensorflow.

The format of the image file to be saved can be specified by the file_format parameter, but if omitted, it is determined from the extension of the file name.

    image_pil = load_img(path)

7-5 Save image

Image data might be treaded as a Numpy array of the element type 'float32' and value range [0.0, 1.0] or [-1.0, 1.0]. If the range of element is [-1.0, 1.0], it is necessary to be converted to the range of [0.0, 1.0] when displaying or saving it.

To save it, use the save_img() function of 'tensorflow.keras'.