实现图片和arr之间的转换(18)---《深度学习》

来源:互联网 发布:网络发帖推广怎么发 编辑:程序博客网 时间:2024/06/06 01:12
import osfrom PIL import Imageimport numpy as npdef jpg_2_arr(file):    img=Image.open(file)    r,g,b=img.split()    r_arr=np.array(r).reshape(112*112)    g_arr=np.array(g).reshape(112*112)    b_arr=np.array(b).reshape(112*112)    img_arr=np.concatenate((r_arr,g_arr,b_arr))    result=img_arr.reshape((3,112,112))    return resultdef arr_2_jpg(arr):    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))    img.save('/home/jobs/Desktop/hello.jpg')if __name__=="__main__":    file="/home/jobs/Pictures/test_01/ambulance_396.png"    ret=jpg_2_arr(file)    arr_2_jpg(ret)

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

原创粉丝点击