废话少说,上代码:
1 import matplotlib as mpl 2 mpl.use(‘Agg‘) 3 4 import os 5 import matplotlib.pyplot as plt 6 from scipy.misc import imresize 7 8 # root path depends on your computer 9 root1 = ‘/share1/home/cong/Datasets/Polyvore/pair1/pair1/‘ 10 save_root1 = ‘/share1/home/cong/Datasets/Polyvore64/pair1/‘ 11 root2 = ‘/share1/home/cong/Datasets/Polyvore/pair2/pair2/‘ 12 save_root2 = ‘/share1/home/cong/Datasets/Polyvore64/pair2/‘ 13 resize_size = 64 14 15 if not os.path.isdir(save_root1): 16 os.makedirs(save_root1) 17 if not os.path.isdir(save_root1 + ‘pair1‘): 18 os.makedirs(save_root1 + ‘pair1‘) 19 img_list1 = os.listdir(root1) 20 21 if not os.path.isdir(save_root2): 22 os.makedirs(save_root2) 23 if not os.path.isdir(save_root2 + ‘pair2‘): 24 os.makedirs(save_root2 + ‘pair2‘) 25 img_list2 = os.listdir(root2) 26 27 # Core Code 28 for i in range(len(img_list1)): 29 img = plt.imread(root1 + img_list1[i]) 30 img = imresize(img, (resize_size, resize_size)) 31 plt.imsave(fname=save_root1 + ‘pair1/‘ + 32 img_list1[i][:-4] + ‘.png‘, arr=img) 33 34 if (i % 1000) == 0: 35 print(‘%d images complete‘ % i) 36 37 for i in range(len(img_list2)): 38 img = plt.imread(root2 + img_list2[i]) 39 img = imresize(img, (resize_size, resize_size)) 40 plt.imsave(fname=save_root2 + ‘pair2/‘ + 41 img_list2[i][:-4] + ‘.png‘, arr=img) 42 43 if (i % 1000) == 0: 44 print(‘%d images complete‘ % i)