python单通道与三通道互转

python单通道与三通道互转,第1张

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录 前言一、pandas是什么?二、使用步骤 1.引入库2.读入数据总结


前言

用PIL.Image 来转换通道


提示:以下是本篇文章正文内容,下面案例可供参考

一、单通道转三通道
#单通道转三通道
def one_to_three():
    img_path = r'./test_imgs'
    save_img_path = r'./test_imgs'
    for file in os.listdir(img_path):
        image = Image.open(os.path.join(img_path, file))
        image=image.convert("RGB")
        image.save(os.path.join(save_img_path, file))
二、三通道转单通道
def three_to_one():
    img_path = r'./test_imgs'
    save_img_path = r'./test_imgs'
    for file in os.listdir(img_path):
        image = Image.open(os.path.join(img_path, file))
        image=image.convert("L")
        image.save(os.path.join(save_img_path, file))
三、检查
def check():
    img_path = r'./test_imgs'
    print("begin:")
    for file in os.listdir(img_path):
        image = Image.open(os.path.join(img_path, file))
        print(np.array(image).shape)

总结
import cv2
from PIL import Image
import os
import numpy as np

def one_to_three():
    img_path = r'./test_imgs'
    save_img_path = r'./test_imgs'
    for file in os.listdir(img_path):
        image = Image.open(os.path.join(img_path, file))
        image=image.convert("RGB")
        image.save(os.path.join(save_img_path, file))

def three_to_one():
    img_path = r'./test_imgs'
    save_img_path = r'./test_imgs'
    for file in os.listdir(img_path):
        image = Image.open(os.path.join(img_path, file))
        image=image.convert("L")
        image.save(os.path.join(save_img_path, file))

def check():
    img_path = r'./test_imgs'
    print("begin:")
    for file in os.listdir(img_path):
        image = Image.open(os.path.join(img_path, file))
        print(np.array(image).shape)

if __name__=="__main__":
    one_to_three()
    check()

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/756166.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-30
下一篇 2022-04-30

发表评论

登录后才能评论

评论列表(0条)

保存