合并3张灰度图为1张有RGB通道的彩色图

import datetime
start=datetime.datetime.now()
import os

path='F:\\timit-all-files\\data\\mergin\\plp\\'
#其中一个灰度图的路径,文件名以png结尾,这里我的三个灰度图分别在三个文件中,并且文件名类似,规律如下:第一个文件夹中文件名name,第二个:name(1),name(2)

def get_filelist(dir):#这个是遍历文件夹的函数,最后的Filelist包含了文件的绝对路径
    Filelist = []
    for home, dirs, files in os.walk(path):
        for filename in files:
            # 文件名列表,包含完整路径
            Filelist.append(os.path.join(home, filename))
            # # 文件名列表,只包含文件名
            # Filelist.append( filename)
    return Filelist

# 三通道合并
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np

if __name__ == "__main__":
    Filelist = get_filelist(dir)
    print(len(Filelist))
    print(Filelist)
    for file in Filelist:
        # print(file)
        res = file.split('\\')
        plp_img_Name=res[-1]
        without_png=plp_img_Name.strip('.png')
        print('without_png name',without_png)
        try:
            img = Image.open(f'{path}{plp_img_Name}')
            img1=Image.open(f'F:\\timit-all-files\\data\\mergin\\\delta\\{without_png}'+'(1).png')
            img2=Image.open(f'F:\\timit-all-files\\data\\mergin\\\delta-delta\\{without_png}'+'(2).png')
            new=Image.merge('RGB',[img,img1,img2])
            print('new',new)
            new.save(f'F:\\timit-all-files\\data\\mergin\\new\\{plp_img_Name}')
        except FileNotFoundError as e:#这里是如果没有在三个文件当中找到同名的文件,就打印出来错误信息
            print(e)


        # print(newFrontFileName)
        # print(oldName)

其实个人的代码写的挺菜的,

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值