Python_code_使用ImageFilter库对图片实现数据增强_28种
代码:
import os
import glob
from PIL import Image
from PIL import ImageFilter
from PIL import ImageEnhance
import matplotlib.pyplot as plt
#以下两行实现了在plt画图时,可以输出中文字符
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#对图片进行数据增强
def Image_Enhancement_img(img):
print(img) #输出图片的路径
#读取图像
im = Image.open(img)
#显示图像
#plt.figure()
#plt.subplot(121),plt.title("原图"),plt.imshow(im),plt.xticks([]),plt.yticks([]);
#1. 对图像进行颜色通道的变换(5种;1-5)
r,g,b=im.split() #获得RGB通道数据
#rbg
om = Image.merge("RGB",(r,b,g)) #合并通道,参数:(色彩,新的色彩通道)
new_filename = img[:-4] + '_rbg.png' #print(new_filename)
om.save(new_filename)
#plt.subplot(122),plt.title("rbg"),plt.imshow(om),plt.xticks([]),plt.yticks([]);
#gbr
om = Image.merge("RGB",(g,b,r))
new_filename = img[:-4] + '_gbr.png'
om.save(new_filename)
#plt.subplot(122),plt.title("gbr"),plt.imshow(om),plt.xticks([]),plt.yticks([]);
#grb
om = Image.merge("RGB",(g,r,b))
new_filename = img[:-4] + '_0.png'
om.save(new_filename)
#plt.subplot(122),plt.title("grb"),plt.imshow(om),plt.xticks([]),plt.yticks([]);
#bgr
om = Image.merge("RGB",(b,g,r))
new_filename = img[:-4] + '_1.png'
om.save(new_filename)
#plt.subplot(122),plt.title("bgr"),plt.imshow(om),plt.xticks([]),plt.yticks([]);
#brg
om = Image.merge("RGB",(b,r,g))
new_filename = img[:-4] + '_2.png'
om.save(new_filename)
#plt.subplot(122),plt.title("brg"),plt.imshow(om),plt.xticks([]),plt.yticks([]);
#第二部分:非常规的数据增强
om = im.filter(ImageFilter.DETAIL)
new_filename = img[:-4] + '_细节.png'
om.save(new_filename)
#plt.subplot(122),plt.title("细节"),plt.imshow(om),plt.xticks([]),plt.yticks([]);
om = im.filter(ImageFilter.SMOOTH)
new_filename = img[:-4