{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Convert to grayscale using the apply method\n", "===========================================\n", "\n", "This example shows how to use `squidpy.ImageContainer.apply` to convert\n", "an image layer to grayscale. This calls `squidpy.im.process` in the\n", "background.\n", "\n", "We take the mean of the three channels(RGB) using the user-defined\n", "function `` `rgb2gray ``[ to convert the image to grayscale. The\n", "function ]{.title-ref}`rgb2gray`[ computes the mean of the three\n", "channels (RGB) across axis 3. The purpose of this example is to show how\n", "the ]{.title-ref}`apply`[ method\n", "{meth}\\`squidpy.ImageContainer.apply]{.title-ref} can be used.\n", "\n", ":::{seealso}\n", "- {doc}`compute_smooth` \n", "- {doc}`compute_process_hires` \n", "- {doc}`compute_gray`\n", "\n", ":::\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "import squidpy as sq" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "First, we load the H&E stained tissue image. Here, we only load a\n", "cropped dataset to speed things up. In general,\n", "{func}`squidpy.im.ImageContainer.apply` can also process very large images.\n", "(See {doc}`compute_process_hires`).\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "28170875559b42c5bdb56cf04e6be2a6", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0.00/35.6M [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fig, axes = plt.subplots(1, 2)\n", "img.show(ax=axes[0])\n", "_ = axes[0].set_title(\"Original\")\n", "gray.show(cmap=\"gray\", ax=axes[1])\n", "_ = axes[1].set_title(\"Grayscale\")\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.9.16" } }, "nbformat": 4, "nbformat_minor": 0 }