Convert to grayscale¶
This example shows how to use
squidpy.im.process() to convert an image layer to grayscale.
import squidpy as sq import matplotlib.pyplot as plt
First, we load the H&E stained tissue image.
Here, we only load a cropped dataset to speed things up.
squidpy.im.process() can also process very large images
(see Process a high-resolution image).
img = sq.datasets.visium_hne_image_crop()
Then, we convert the image to grayscale and plot the result.
With the argument
layer we can select the image layer that should be processed.
When converting to grayscale, the channel dimensions change from 3 to 1.
By default, the name of the resulting channel dimension will be
Use the argument
channel_dim to set a new channel name explicitly.
By default, the resulting image is saved in the layer
This behavior can be changed with the arguments
sq.im.process(img, layer="image", method="gray") fig, axes = plt.subplots(1, 2) img.show("image", ax=axes) _ = axes.set_title("original") img.show("image_gray", cmap="gray", ax=axes) _ = axes.set_title("grayscale")
Total running time of the script: ( 0 minutes 8.630 seconds)
Estimated memory usage: 626 MB