一、对数据集进行重命名—按顺序编号
# -*- coding:utf8 -*-
#coding:UTF-8
import os
class BatchRename():
'''
批量重命名文件夹中的图片文件
'''
def __init__(self):
self.path = 'C:\CBSD68'
def rename(self):
filelist = os.listdir(self.path)
total_num = len(filelist)
i = 1
for item in filelist:
if item.endswith('.png' ) :
src = os.path.join(os.path.abspath(self.path), item)
dst = os.path.join(os.path.abspath(self.path), str(i) + '.png')
try:
os.rename(src, dst)
print ('converting %s to %s ...' % (src, dst))
i = i + 1
except:
continue
print ('total %d to rename & converted %d jpgs' % (total_num, i))
if __name__ == '__main__':
demo = BatchRename()
demo.rename()
二、对数据集进行修改图片大小
#coding:UTF-8
from PIL import Image
import os.path
import glob
def convertjpg(jpgfile,outdir,width=260,height=260):
img=Image.open(jpgfile)
try:
new_img=img.resize((width,height),Image.BILINEAR)
new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
except Exception as e:
print(e)
for jpgfile in glob.glob(r"C:\Users\86150\Desktop\datasets\ImageNet400\*.png"): #读取文件
convertjpg(jpgfile,r"C:\Users\86150\Desktop\datasets\IMage280") #保存文件位置
三、将数据集彩色图片转换为灰度图像
#coding:UTF-8
from skimage import io,transform,color
import numpy as np
def convert_gray(f,**args):#图片处理与格式化的函数
rgb=io.imread(f) #读取图片
gray=color.rgb2gray(rgb) #将彩色图片转换为灰度图片
#dst=transform.resize(gray,(64,64)) #调整大小,图像分辨率为64*64
return gray
datapath = r'C:\Users\86150\Desktop\datasets\CImageNet400'
str=datapath+'/*.png' #识别.jpg的图像
coll = io.ImageCollection(str,load_func=convert_gray)#批处理
for i in range(len(coll)):
io.imsave(r'C:/Users/86150/Desktop/datasets/ImageNet400/'+np.str(i)+'.png',coll[i]) #保存图片在d:/daate/date/