获取文件名+后缀并重命名

glob获取路径,拆分获取文件名和后缀和路径,读取图像后改变大小。

from PIL import Image
import matplotlib.pyplot as plt
from skimage import data, exposure, img_as_float
import cv2
import glob as gb
import scipy
from glob import glob

import os

test_path = 'E:/PycharmProject2018/StarGAN-Tensorflow-masterd/dataset/celebA/test'
test_files = glob(os.path.join(test_path , '*.*'))
i = 1
for sample_file in test_files:
    filepath, tmpfilename = os.path.split(sample_file)
    print(sample_file)
    img = scipy.misc.imread(sample_file, mode='RGB')
    print(img.shape)
    h, w, c = img.shape
    img = scipy.misc.imresize(img, [h, w])
    tmpfilename = os.path.basename(sample_file)
    shotname, extension = os.path.splitext(tmpfilename)
    print(shotname)
    shotname = shotname + '(' + str(i) + ')' + extension
    i = i + 1
    print(shotname)
    print(extension)
    scipy.misc.imsave(os.path.join(filepath, shotname), img)

 

;