转自
http://www.jianshu.com/p/a76c18a3c6d5
做这种处理是为了使用VGG模型,将训练集和测试集转化为jpg格式
import os
import cv2
import sysdef
convert_gray_img_to_rgb(base_dir,dir_pre_str,dir_range_list,dir_post_str,file_format,partion_list):
for i in dir_range_list:
for index,partion_list_part in enumerate(partion_list):
for k in partion_list_part:
if base_dir=="":
base_dir_str=""
else:
base_dir_str=base_dir+os.sep
type=""
if index==0:
type="train"
elif index==1:
type="tst"
file_input_path=base_dir_str+type+os.sep+dir_pre_str+str(i)+dir_post_str+os.sep+str(k)+file_format
img = cv2.imread( file_input_path,0 )
img = cv2.cvtColor( img, cv2.COLOR_GRAY2RGB )
out_file= base_dir_str+type+os.sep+dir_pre_str+str(i)+dir_post_str+os.sep+str(k)+".jpg"
cv2.imwrite(out_file, img)
if __name__=='__main__':
source_dir="/Users/Ren/Downloads/att_faces_back"
dir_pre_str="s"
dir_range_list=range(1,41)
test_partion_list=[7,8,9,10]
train_partion_list=[1,2,3,4,5,6]
dir_post_str=""
file_format=".pgm"
convert_gray_img_to_rgb(source_dir,dir_pre_str,dir_range_list,dir_post_str,file_format,[train_partion_list,test_partion_list])