# coding=utf-8 # author: heimu ''' 功能: 图片灰度化,并且resize图片 亮点: 可以实现自动统计文件夹内的图片个数,也就是说不同的图片文件夹的图片个数不一样 时间: 2018.4.12 ''' from PIL import Image import numpy as np import os path = '/home/heimu/PycharmProjects/env27_brian/army-image-400/data/AerialImg/' number_image = 0 for i in range(8): first_path = path + 'Aerial_train_data/' + str(i) + '/' first_path_save = path + 'train/' + str(i) + '/' count = 0 # 下面这个循环就是统计文件夹内的图片数量,然后保存到count中 for filename in os.listdir(first_path): count+=1 print('------count:',count) for j in range(count): whole_dir = first_path + str(j+1) + '.png' whole_dir_save = first_path_save + str(j+1) + '.bmp' print(str(j) + '.png') img = Image.open(whole_dir) img_gray = img.convert('L') img_gray_resize = img_gray.resize((150,150)) img_gray_resize.save(whole_dir_save) number_image+=1 print('number_image:',number_image) # np.array(img) img-->array # Image.fromarray(array) array-->image