Use pickle image to serialize and read images in Python to restore display images

Source: Internet
Author: User

Python has a variety of learning expansion packages, especially in terms of algorithm packages, such as machine learning, deep learning, etc. are excellent, we can also find in Python image processing related expansion packages such as pil,image, etc. Below we use the olivettifaces face Vault, a mini dataset with only 400 faces for a total of 40 people, to show you how to save a picture as a large array of grayscale values, save it as a pkl file, and then restore the array sequence back to a single picture.


The total size of the picture is 1140*942, there are 400 face charts, each person has 10 different angles of the face, a total of 40 different people, so each face map size (1140/20) * (942/20) that is 57*47=2679, our goal is to save these pictures in a 400* 2679 of the array, each row represents a face, each face map is saved as a 2679-dimensional one-dimensional vector;


From the picture we can observe that there are total 20 rows and 20 columns, 20 columns have two people, 10 pictures of different angles each, 20 rows of each row are different people;

The following is an implementation of the Python code:

Import numpy from PIL import Image import pickle img = Image.open (' C:\Python34\olivettifaces.gif ') #将图片转化为ndarra Y Gray degree Group Img_ndarray = Numpy.asarray (img, dtype= ' float64 ')/256 #将灰度值由0 ~256 converted to 0~1 #图片总大小为1140 *942, altogether 400 human face graphs, Therefore, each face map size is (1140/20) * (942/20) 57*47=2679 #将全部400个样本存储为一个400 *2679 Array, each row represents a face, and the No. 0 to 9th, 10~19 line belong to the same personal face #另外,  The class of each sample is represented by Olivettifaces_label, which is a vector of 400 dimensions, with 0~39 total of 40 classes representing 40 different people. Olivettifaces=numpy.empty ((400,2679)) #开辟一个空数组 for row in range: for column in range: #从第一行开始 first row There are 20 faces two people olivettifaces[row*20+column]=numpy.ndarray.flatten (Img_ndarray [row*57: (row+1) *57,column*47: (column+1) *47]) #建立分类标签 Olivettifaces_label=numpy.empty (+) for label in range (+): olivettifaces_label[label*10:label*10 +10]=label #每十个赋值为一类标签olivettifaces_label =olivettifaces_label.astype (numpy.int) #保存olivettifaces以及olivettifaces_ Label to OLIVETTIFACES.PKL file Write_file=open (' C:\Python34\olivettifaces.pkl ', ' WB ') Pickle.dump (olivettifaces,write_ File,-1) Pickle.dump (olivettifaces_label,write_file,-1) # Dump two times write_file.close () Import pylabread_file=open (' C:\Pyt Hon34\olivettifaces.pkl ', ' RB ') Faces=pickle.load (read_file) read_file.close () Img1=faces[1].reshape (57,47) Pylab.imshow (IMG1) Pylab.gray () pylab.show () #将olivettifaces分为训练集 (320 samples), validation set (40 samples), test set (40 samples) Import Pickle Read_ File=open (' C:\Python34\olivettifaces.pkl ', ' RB ') Faces=pickle.load (read_file) # Pickle.load two times label=pickle.load ( Read_file) Read_file.close () Train_data=numpy.empty ((320,2679)) #创建空盒子 divided into three block sizes of 320,40,40train_label=numpy.empty ( Valid_data=numpy.empty ((40,2679)) Valid_label=numpy.empty (+) Test_data=numpy.empty ((40,2679)) test_label= Numpy.empty (+) for I in range (+): #0 0~7 train_data[i*8:i*8+8]=faces[i*10:i*10+8                 ] #8 ~15 10~17 train_label[i*8:i*8+8]=label[i*10:i*10+8] #16 ~23 20~27 valid_data[i]=faces[i*10+8]          #312 ~319 390~397 Valid_label[i]=label[i*10+8]          TEST_DATA[I]=FACES[I*10+9] #8 test_label[i]=label[i*10+9] #18                                                                                                #28 #398                                                #9 #19 #29 #399
Output results We found that the second face graph in the first row is displayed with the number [1].





Reference: http://blog.csdn.net/u012162613/article/details/43226127


Use pickle image to serialize and read images in Python to restore display images

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.