Created
March 12, 2024 10:15
-
-
Save CnrLwlss/3fb905cabb6ceea40c7db5e0086b90ad to your computer and use it in GitHub Desktop.
Making single channel image from multichannel images (RGB in this example).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from PIL import Image | |
# Open a multichannel image | |
# In this example, an RGB image | |
fname = r"C:\Users\Conor\Downloads\PXL_20240304_174814563.MP.jpg" | |
im = Image.open(fname) | |
arr = np.array(im,dtype=np.uint8) | |
# Some different ways to flatten the image to greyscale | |
r,g,b = np.squeeze(np.dsplit(arr,arr.shape[2]),arr.shape[-1]) | |
meanarr = np.array(np.round(np.mean(arr,axis=2)),dtype=np.uint8) | |
medianarr=np.array(np.median(arr,axis=2),dtype=np.uint8) | |
maxarr = np.max(arr,axis=2) | |
minarr = np.min(arr,axis=2) | |
im.save("splitA_original.tif") | |
Image.fromarray(r).save("splitB_R.tif") | |
Image.fromarray(g).save("splitC_B.tif") | |
Image.fromarray(b).save("splitD_B.tif") | |
Image.fromarray(meanarr).save("splitE_Mean.tif") | |
Image.fromarray(medianarr).save("splitF_Median.tif") | |
Image.fromarray(maxarr).save("splitG_Max.tif") | |
Image.fromarray(minarr).save("splitH_Min.tif") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment