Last active
June 30, 2023 15:51
-
-
Save liuhh02/ade48d822139006f98d8f05681209ab6 to your computer and use it in GitHub Desktop.
Find number of channels in your image
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
""" | |
num_channels.py | |
Find number of channels in your image. | |
Author: liuhh02 https://machinelearningtutorials.weebly.com/ | |
""" | |
from PIL import Image | |
import numpy as np | |
# name of your image file | |
filename = './directory/to/image/file/image.tif' | |
# open image using PIL | |
img = Image.open(filename) | |
# convert to numpy array | |
img = np.array(img) | |
# find number of channels | |
if img.ndim == 2: | |
channels = 1 | |
print("image has 1 channel") | |
else: | |
channels = image.shape[-1] | |
print("image has", channels, "channels") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I needed to change the image to
img = cv2.imread(filename)
because the img in your code has no attribute ndim