python图像处理PIL实例之读取图片
"""
读取图片实例
"""
from PIL import Image
import numpy as np
import os
import pickle
img_dir1='./images/'
img_dir2='./storage/'
array_file ='./array.bin'
def image_array_file():
imgnames=os.listdir(img_dir1)
img_array = np.array([])
print(imgnames)
for imgname in imgnames:
img=Image.open(img_dir1+imgname)
r,g,b=img.split()
print(r)
r_array=np.array(r).reshape(40000)
g_array =np.array(g).reshape(40000)
b_array =np.array(b).reshape(40000)
arrs=np.concatenate((r_array,g_array,b_array))
img_arrays=np.concatenate(arrs,img_array)
with open(array_file,'wb') as f:
pickle.dump(img_arrays,f)
def file_image():
with open(array_file,'rb') as f:
images=pickle.load(f)
ima_arr=images.reshape(9,3,250,250)
for i in range(9):
r=Image.fromarray(ima_arr[i][0].convert('L'))
g=Image.fromarray(ima_arr[i][1].convert('L'))
b=Image.fromarray(ima_arr[i][2].convert('L'))
image=Image.merge('RGB',(r,g,b))
image.save(img_dir2+'.png')
file_image()