将npy数据集恢复为图片(17)---《深度学习》

来源:互联网 发布:js判断手指滑动距离 编辑:程序博客网 时间:2024/04/24 20:12

代码:

import numpy as npfrom PIL import Imageimport osdir="/home/jobs/Desktop/data_self/"dest_dir="/home/jobs/Desktop/jpg_self/"def npy2jpg(dir,dest_dir):    if os.path.exists(dir)==False:        os.makedirs(dir)    if os.path.exists(dest_dir)==False:        os.makedirs(dest_dir)    file=dir+'test_data.npy'    con_arr=np.load(file)    count=0    for con in con_arr:        arr=con[0]        label=con[1]        print(np.argmax(label))        arr=arr*255        #arr=np.transpose(arr,(2,1,0))        arr=np.reshape(arr,(3,112,112))        r=Image.fromarray(arr[0]).convert("L")        g=Image.fromarray(arr[1]).convert("L")        b=Image.fromarray(arr[2]).convert("L")        img=Image.merge("RGB",(r,g,b))        label_index=np.argmax(label)        img.save(dest_dir+str(label_index)+"_"+str(count)+".jpg")        count=count+1if __name__=="__main__":    npy2jpg(dir,dest_dir)

运行结果:
这里写图片描述

本片博客中所用到的npy数据集就是上篇博客中所产生的npy数据集,以及本片博客中产生的图片都在我的资源页中,大家可以进行下载!

PS:注意上面我们在获取每个通道的数组时首先先将数组进行了reshape操作,然后进行其他操作!

阅读全文
0 0
原创粉丝点击