PIL简单应用

PIL简介

PIL是python自带的图像处理库

安装:

pip install pillow

扩充数据-旋转

准备:新建Train文件夹,将NG和OK图片分别放在命名为NG和OK的文件夹,新建rotation文件夹,或者修改路径

import os
from PIL import Image
import numpy as np

def load_Img(imgDir):
lable = os.listdir(imgDir)
OK_name=os.listdir(imgDir+'/'+lable[0])
NG_name=os.listdir(imgDir+'/'+lable[1])
print(lable)
for i in range(len(OK_name)):
OK_path = imgDir + "/" + lable[0]+'/'+OK_name[i-1]
OK_img = Image.open(OK_path)
OK_img=OK_img.convert('L')
OK_img = OK_img.resize((256,256))
out1 = OK_img.rotate(90) # 逆时针旋转90度
out1.save("E:\\image\\rotation\\{}\\{}_90_{}.bmp".format(lable[0],lable[0],i))
OK_img.save("E:\\image\\rotation\\{}\\{}_{}.bmp".format(lable[0],lable[0],i))

for j in range(len(NG_name)):

NG_path = imgDir + '/' + lable[1] + '/' + NG_name[j - 1]
NG_img = Image.open(NG_path)
NG_img = NG_img.convert('L')#L为灰度
NG_img = NG_img.resize((256,256))#改变大小
out2 = NG_img.rotate(90) # 逆时针旋转90度
out2.save("E:\\image\\rotation\\{}\\{}_90_{}.bmp".format(lable[1],lable[1],j))
NG_img.save("E:\\image\\rotation\\{}\\{}_{}.bmp".format(lable[1],lable[1],j))


craterDir = "E:\image\Train"
rotation=load_Img(craterDir)
------ 本文结束 🎉🎉 谢谢观看 ------
0%