{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "Aw2UIYteKHYi"
},
"source": [
"# matplotlib tutorial (7) nitta@tsuda.ac.jp\n",
"\n",
"# Chapter 7: Saving / loading / Displaying of Images\n",
"\n",
"\"Save to image file\".\n",
"\n",
"For the sake of simplicity, this tutorial will use the image data from the database of tensorflow.keras
.\n",
"\n",
"Google Colab can use tensorflow by default, but if tensorflow is not installed in your jupyter notebook environment, install it whth the pip
command.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "khL7hq7LKDwS"
},
"outputs": [],
"source": [
"## If you use jupyter notebook on your PC, please install 'tensorflow' package.\n",
"# ! pip install tensorflow"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Nu2X7nsxcFtD"
},
"source": [
"
uint8
type arrays of [0, 255] and color images to float32
type arrays of [0, 1], and then apply Axes.imshow()
.\n",
" Parameters:\n",
" X: Numpy Array or PIL image\n",
" Shapes of the supported Numpy array type:\n",
" (M, N) ... a gray scale image. call it with cmap='gray', vmin=0, vmax=255
\n",
" (M, N, 3 or 4) ... a color image. Numpy array with 'float32' type elements of range[0.0, 1.0], or with uint8' type elements of range [0, 255].\n",
"
\n",
"\n",
"\n",
"For a gray scale image (number of channel = 1), convert it to a Numpy array with 'unit8' type elements of range [0, 255].\n",
"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].\n",
"\n",
"Then apply the 'Axes.imshow()' function to the image.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mLIYc8JnKRxG"
},
"source": [
"## 7-1: Display a grayscale image, using Axes.imshow()
\n",
"\n",
"The image displayed must be a 2-dimensional array.\n",
"If the elements of the array are integers, the brightness (0: black, 255: white) is expressed in range [0, 255].\n",
"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].\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 4890,
"status": "ok",
"timestamp": 1648474971166,
"user": {
"displayName": "Yoshihisa Nitta",
"photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgJLeg9AmjfexROvC3P0wzJdd5AOGY_VOu-nxnh=s64",
"userId": "15888006800030996813"
},
"user_tz": -540
},
"id": "Px_7kXi-K6W4",
"outputId": "b252feff-149b-41ec-e0ad-22f1c8f3e879"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.8.0\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"\n",
"print(tf.__version__)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 736,
"status": "ok",
"timestamp": 1648474971897,
"user": {
"displayName": "Yoshihisa Nitta",
"photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgJLeg9AmjfexROvC3P0wzJdd5AOGY_VOu-nxnh=s64",
"userId": "15888006800030996813"
},
"user_tz": -540
},
"id": "7ddY9-UHLU0w",
"outputId": "754239e3-59b3-4b70-e186-dcf98431637b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n",
"11493376/11490434 [==============================] - 0s 0us/step\n",
"11501568/11490434 [==============================] - 0s 0us/step\n",
"(60000, 28, 28) (60000,) (10000, 28, 28) (10000,)\n"
]
}
],
"source": [
"# Prepare the image data of MNIST (handwritten characters)\n",
"\n",
"(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()\n",
"\n",
"print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 4,
"status": "ok",
"timestamp": 1648474971897,
"user": {
"displayName": "Yoshihisa Nitta",
"photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgJLeg9AmjfexROvC3P0wzJdd5AOGY_VOu-nxnh=s64",
"userId": "15888006800030996813"
},
"user_tz": -540
},
"id": "lpS4Y7qxR7gB",
"outputId": "53d808f4-fbbb-4aec-d516-e64d53005634"
},
"outputs": [
{
"data": {
"text/plain": [
"numpy.uint8"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Examine the types of elements of the Numpy array of a gray scale image.\n",
"type(x_train[0][0][0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 173
},
"executionInfo": {
"elapsed": 417,
"status": "ok",
"timestamp": 1648474972312,
"user": {
"displayName": "Yoshihisa Nitta",
"photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgJLeg9AmjfexROvC3P0wzJdd5AOGY_VOu-nxnh=s64",
"userId": "15888006800030996813"
},
"user_tz": -540
},
"id": "U-7Kr-HxL2ij",
"outputId": "edfac6a3-6ece-4608-91e0-546216bd091f"
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAUYAAACcCAYAAAADBQINAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAIzElEQVR4nO3dS0hUfRjHcSelq5W5KUsibGE3SsIUJDITkyANq0VGBBXhQrOFGmGLaKEEVgvThdAFCoqgGCohSrKMECwpu0kukojE2piV2gV13sW7eF+e50lP43GcGb+f5W9mzvlX04+Dj/9zPD6fLwIA8J8pE70AAAg2FCMACBQjAAgUIwAIFCMACBQjAAhRI73o8Xj4XR74xefzeQJ9ygCfD+FDfVe5YgQAgWIEAIFiBACBYgQAgWIEAIFiBACBYgQAgWIEAIFiBACBYgQAgWIEAIFiBACBYgQAgWIEAIFiBACBYgQAgWIEAIFiBACBYgQAgWIEAIFiBACBYgQAgWI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