{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## geom_imshow() and geom_raster()\n", "\n", "`geom_imshow()` displays an image specified by 2D or 3D Numpy array.\n", "\n", "Whether the image is grayscale or color depends on the shape of the image array:\n", "- (M, N) - grey-scale image\n", "- (M, N, 3) - color RGB image\n", "- (M, N, 4) - color RGB image with alpha channel\n", "\n", "For example, you can create an RGB image array from a PNG file using `PIL` and `io` packages.\n", "\n", "`geom_raster()` on the other hand is a regular `ggplot` graphic layer wich takes a data-frame as the input \n", "and applies the aesthetic mappings as specified by the `aes()` function.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(225, 225, 3)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "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", "\n", "image = Image.open(BytesIO(response.content))\n", "img = np.asarray(image)\n", "img.shape\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### RGB image\n", "\n", "M x N x 3 `ndarray` specifies a color RGB image.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_imshow(img)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Grayscale image\n", "\n", "M x N `ndarray` specifies a grayscale image.\n", "\n", "We will generate a grayscale image from RGB image above\n", "by selecting only its `R` channel.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(225, 225)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img_gs = img[:,:,0]\n", "img_gs.shape" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_imshow(img_gs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Grayscale image in pseudo colors (cmap)\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_imshow(img_gs, cmap=\"magma\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_raster()`\n", "\n", "The `geom_raster()` function also can display image. But unlike `geom_imshow()`, `geom_raster()` doesn't accept `ndarray` as an input.\n", "\n", "As a regular `ggplot` graphic layer, `geom_raster()` requires an observations-style input data and aestetic mapping.\n", "\n", "This is not very convenient when the data is originally in the form of `ndarray`. On the other hand, aesthetic mapping and color scales might come in handy when rendering an image.\n", "\n", "Lets transform the image data to a data-frame suitable for `geom_raster()`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# reshape 3-dimentional ndarray to 2-dimentional long-form ndarray\n", "# and then to data frame with columns x,y,r,g,b\n", "cols, rows, _ = img.shape\n", "img_lf = img.reshape(cols * rows, -1)\n", "img_df = pd.DataFrame(img_lf,columns=['r','g','b'])\n", "X_mesh, Y_mesh = np.meshgrid(np.arange(rows), np.arange(cols))\n", "img_df['x'] = X_mesh.reshape(-1)\n", "img_df['y'] = Y_mesh.reshape(-1)[::-1]\n", "\n", "# Pack color components values to 24-bit RGB values \n", "c_fill = img_df.apply(lambda row: ((int(row['r'] * 255) << 16) + \n", " (int(row['g'] * 255) << 8) +\n", " int(row['b'] * 255)),\n", " axis=1)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show image in pseudo colors with only few gradations \n", "p = (ggplot(img_df) + geom_raster(aes('x', 'y', fill=c_fill), show_legend=False)\n", " + scale_fill_brewer()\n", " + ggtitle('Raster geom with brewer palette') + ggsize(800,500))\n", "p" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Fontconfig warning: \"/home/ikupriyanov/.fonts.conf\", line 11: invalid attribute 'name'\n" ] }, { "data": { "text/plain": [ "'/home/ikupriyanov/Projects/lets-plot/docs/f-22d/lets-plot-images/raster.bmp'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "Unsupported attribute `fill-rule` in Path\n" ] } ], "source": [ "ggsave(p, \"raster.bmp\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "RGBEncoderNative.toDataUrl: width=225, height=225, argbValues.size=50625\n" ] }, { "data": { "text/plain": [ "'/home/ikupriyanov/Projects/lets-plot/docs/f-22d/lets-plot-images/blank.svg'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggsave(p, \"blank.svg\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.11" } }, "nbformat": 4, "nbformat_minor": 4 }