Grayscale pictures and resize pictures

# coding=utf-8
# author: heimu

'''
Function: Grayscale pictures and resize pictures
Highlights: The number of pictures in the folder can be automatically counted, that is to say, the number of pictures in different picture folders is different
Time: 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

    #The following loop is to count the number of pictures in the folder, and then save it to 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
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324593416&siteId=291194637