Skip to content

Instantly share code, notes, and snippets.

@stephengruppetta
Created November 22, 2023 23:19
Show Gist options
  • Save stephengruppetta/cc21e1e29cf17a3aa3bfa456f7bb5342 to your computer and use it in GitHub Desktop.
Save stephengruppetta/cc21e1e29cf17a3aa3bfa456f7bb5342 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
image = plt.imread("gardens.jpg")
# Create a figure
fig, axs = plt.subplot_mosaic(
[
["colour", "colour", "colour", "hist red"],
["colour", "colour", "colour", "hist green"],
["colour", "colour", "colour", "hist blue"],
["red", "green", "blue", "."],
]
)
axs["colour"].imshow(image)
axs["colour"].axis("off")
# Create red channel image. I'll keep these images as 3D arrays
# and set the second and third layers, which represent the green
# and blue channels, to 0
image_red = image.copy()
image_red[:, :, 1:] = 0
# Same for the green image, but now the first and third layers
# are set to 0
image_green = image.copy()
image_green[:, :, 0] = 0
image_green[:, :, 2] = 0
image_blue = image.copy()
image_blue[:, :, 0:2] = 0
# Plot the separate channels
axs["red"].imshow(image_red)
axs["red"].axis("off")
axs["green"].imshow(image_green)
axs["green"].axis("off")
axs["blue"].imshow(image_blue)
axs["blue"].axis("off")
# Plot the histograms
axs["hist red"].hist(image[:, :, 0].ravel(), bins=50, color="red")
axs["hist red"].axis("off")
axs["hist green"].hist(image[:, :, 1].ravel(), bins=50, color="green")
axs["hist green"].axis("off")
axs["hist blue"].hist(image[:, :, 2].ravel(), bins=50, color="blue")
axs["hist blue"].axis("off")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment