下面展示 getAugmentionJPG.py
展示pillow的数据增强功能
import os
import shutil
from PIL import Image
from PIL import ImageFilter
sourcePath = "train"
# sourcePath = "test"
destinationPath_CONTOUR = sourcePath + "_CONTOUR/"
destinationPath_DETAIL = sourcePath + "_DETAIL/"
destinationPath_EDGE_ENHANCE = sourcePath + "_EDGE_ENHANCE/"
destinationPath_EMBOSS = sourcePath + "_EMBOSS/"
destinationPath_FIND_EDGES = sourcePath + "_FIND_EDGES/"
destinationPath_SHARPEN = sourcePath + "_SHARPEN/"
for filename in os.listdir(sourcePath): # 2020-03-13_07#59#45.jpg label.txt
fileNameStr = str(filename)
if fileNameStr.endswith(".txt"):
shutil.copyfile(os.path.join(sourcePath,fileNameStr), os.path.join(destinationPath_CONTOUR,fileNameStr))
shutil.copyfile(os.path.join(sourcePath,fileNameStr), os.path.join(destinationPath_DETAIL,fileNameStr))
shutil.copyfile(os.path.join(sourcePath,fileNameStr), os.path.join(destinationPath_EDGE_ENHANCE,fileNameStr))
shutil.copyfile(os.path.join(sourcePath,fileNameStr), os.path.join(destinationPath_EMBOSS,fileNameStr))
shutil.copyfile(os.path.join(sourcePath,fileNameStr), os.path.join(destinationPath_FIND_EDGES,fileNameStr))
shutil.copyfile(os.path.join(sourcePath,fileNameStr), os.path.join(destinationPath_SHARPEN,fileNameStr))
# print(fileNameStr)
if fileNameStr.endswith(".jpg"):
fileNameStrStripSuffix = fileNameStr[:-4]
im = Image.open(os.path.join(sourcePath,fileNameStr))
# print("图片读取成功...")
im_CONTOUR = im.filter(ImageFilter.CONTOUR)
im_CONTOUR.save( destinationPath_CONTOUR+fileNameStr)
im_DETAIL = im.filter(ImageFilter.DETAIL)
im_DETAIL.save( destinationPath_DETAIL+fileNameStr)
im_EDGE_ENHANCE = im.filter(ImageFilter.EDGE_ENHANCE)
im_EDGE_ENHANCE.save( destinationPath_EDGE_ENHANCE+fileNameStr)
im_EMBOSS = im.filter(ImageFilter.EMBOSS)
im_EMBOSS.save( destinationPath_EMBOSS+fileNameStr)
im_FIND_EDGES = im.filter(ImageFilter.FIND_EDGES)
im_FIND_EDGES.save( destinationPath_FIND_EDGES+fileNameStr)
im_SHARPEN = im.filter(ImageFilter.SHARPEN)
im_SHARPEN.save( destinationPath_SHARPEN+fileNameStr)
# print(newName) # 2020-03-13_07#59#45.jpg label.txt
# newName = newName.replace(' ', '')
# os.rename(os.path.join(sourcePath,filename),os.path.join(sourcePath,newName))
# print("文件: ",filename,"--->",newName," 重命名已完成!")