from PIL import Image
import numpy as np
from PIL import Image
from keras.preprocessing import image
import matplotlib.pyplot as plt
import os
import cv2
# from scipy.misc import toimage
import matplotlib
# 生成图片地址和对应标签
file_dir = '../train/'
image_list = []
label_list = []
cate = [file_dir + x for x in os.listdir(file_dir) if os.path.isdir(file_dir + x)]
for name in cate:
temp = name.split('/')
path = '../train_new/' + temp[-1]
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path) # 目录不存在则创建
class_path = name + "/"
for file in os.listdir(class_path):
print(file)
img_obj = Image.open(class_path + file) # 读取图片
img_array = np.array(img_obj)
resized = cv2.resize(img_array, (256, 256)) # 裁剪
resized = resized.astype('float32')
resized /= 255.
# plt.imshow(resized)
读取图片成numpy数组,裁剪并保存 和 数据增强(ImageDataGenerator)
最新推荐文章于 2022-04-16 10:28:37 发布