首页 > 解决方案 > 减少python中的图像通道

问题描述

我有一个尺寸为 (128, 19, 3) 的图像,我想将其转换为 (128, 19, 1)。我使用了这段代码(在 python 中),但它将图像大小转换为 (128, 19) 而不是我想要的 (128, 19, 1)。谢谢,如果有人可以帮忙

from PIL import Image
import glob
images = glob.glob('D:\\thesis\\Paper 3\\Feature 
Extraction\\two_dimension_Feature_extraction\\stft_feature\\Training_set\\P300\\*.png')
img = Image.open(images[0]).convert('L')

标签: pythonimagenumpyrgb

解决方案


只需在数组末尾添加一个维度:

img = img[...,None]

推荐阅读