【Numpy应用】--对于图片处理的机器学习库的应用

一。思路

 

二。代码:

#coding:utf-8
import numpy as np
import PIL.Image as Image
import pickle as p
import  os

class ImageTools(object):

    image_dir='images/'
    result_dir='results/'
    data_file_path='data.bin'
    def imageToArray(self,files):
        images=[]
        for i in range(len(files)):
            image=Image.open(ImageTools.image_dir+files[i])
            r,g,b= image.split()
            r_array = np.array(r).reshape(62500)
            g_array = np.array(g).reshape(62500)
            b_array = np.array(b).reshape(62500)
            image_arry = np.concatenate((r_array,g_array,b_array))
            images=np.concatenate((images,image_arry))
        print(images.shape)
        images =np.array(images).reshape((len(files),(3*62500)))
        print(images.shape)
        f =open(ImageTools.data_file_path,'wb')
        p.dump(images,f)
        f.close()

    def readToImage(self,file):
        f = open(file,'rb')
        arr = p.load(f) # 30行,187500列
        rows = arr.shape[0]

        new_arr =arr.reshape((rows,3,250,250)) # 把矩阵变成一个高维矩阵

        for i in range(rows):
            r =Image.fromarray(new_arr[i][0]).convert("L") #把每个图片中RGB通道分离
            g =Image.fromarray(new_arr[i][1]).convert("L") #把每个图片中RGB通道分离
            b =Image.fromarray(new_arr[i][2]).convert("L") #把每个图片中RGB通道分离

            image = Image.merge("RGB",(r,g,b)); # 合并RGB通道获得一张图片
            # f =open(ImageTools.result_dir+str(i)+'.png','wb')
            image.save(ImageTools.result_dir+str(i)+'.png',"png")
if __name__=="__main__":
    it = ImageTools()
    files= os.listdir(ImageTools.image_dir)
    # it.imageToArray(files)
it.readToImage('data.bin')

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

L先生AI课堂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值