把其他通道值降为0,来显示单独一个通道
from PIL import Image
import numpy as np
def process_and_resize_image(input_path, output_path):
# 打开PNG图像
img = Image.open(input_path)
# 获取图像的像素数据
pixels = np.array(img)
# 将G和B通道的像素值设为0
pixels[:, :, 1] = 0 # G通道
pixels[:, :, 2] = 0 # B通道
# 将图像尺寸调整为512x512
resized_img = Image.fromarray(pixels).resize((512, 512))
# 保存处理后的图像
resized_img.save(output_path)
print(f"处理后的图像已保存到 {output_path}")
# 替换 'input_image.png' 和 'output_resized_image.png' 为你的实际文件名
process_and_resize_image('1.png', '11.png')