{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Image Matrix\n", "\n", "The `image_matrix()` function arranges a set of images in a grid.\n", "\n", "Dimensions of the grid are determined by the shape of the input Numpy 2D array.\n", "\n", "Each element of the input 2D array is an 2D or 3D Numpy array itself specifying either a grayscale image (2D array) \n", "or a color RGB(A) image (3D array).\n", "\n", "For more information on image arrays please see the documentation of `geom_imshow()` function.\n", "\n", "The `image_matrix()` function uses `geom_imshow()` under the hood, so you may want to check these demos as well:\n", "\n", " - [image_101.ipynb](https://nbviewer.org/github/JetBrains/lets-plot-docs/blob/master/source/examples/cookbook/image_101.ipynb)\n", " - [image_fisher_boat.ipynb](https://nbviewer.org/github/JetBrains/lets-plot-docs/blob/master/source/examples/cookbook/image_fisher_boat.ipynb)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:43.165534Z", "iopub.status.busy": "2024-04-17T07:34:43.165457Z", "iopub.status.idle": "2024-04-17T07:34:43.492812Z", "shell.execute_reply": "2024-04-17T07:34:43.492352Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "from lets_plot import *\n", "from lets_plot.bistro.im import image_matrix\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:43.505548Z", "iopub.status.busy": "2024-04-17T07:34:43.505415Z", "iopub.status.idle": "2024-04-17T07:34:43.852959Z", "shell.execute_reply": "2024-04-17T07:34:43.852531Z" } }, "outputs": [ { "data": { "text/plain": [ "(225, 225, 3)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Load image file.\n", "from PIL import Image\n", "import requests\n", "from io import BytesIO\n", "\n", "response = requests.get('https://github.com/JetBrains/lets-plot-docs/raw/master/source/examples/cookbook/images/fisher_boat.png')\n", "image = Image.open(BytesIO(response.content))\n", "img = np.asarray(image)\n", "img.shape\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create 2 x 3 grid of images\n", "\n", "Use the same image to fill the array elements." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:43.854290Z", "iopub.status.busy": "2024-04-17T07:34:43.854201Z", "iopub.status.idle": "2024-04-17T07:34:43.856058Z", "shell.execute_reply": "2024-04-17T07:34:43.855776Z" } }, "outputs": [], "source": [ "M = 2 # rows\n", "N = 3 # columns\n", "X = np.empty([M, N], dtype=object)\n", "X.fill(img)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Display images in 2 x 3 grid" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:43.857056Z", "iopub.status.busy": "2024-04-17T07:34:43.856935Z", "iopub.status.idle": "2024-04-17T07:34:43.947754Z", "shell.execute_reply": "2024-04-17T07:34:43.947541Z" } }, "outputs": [ { "data": { "text/html": [ " \n", " " ], "text/plain": [ "