resize给文件重新命名和大小
2018-01-14 本文已影响0人
sunnylxs
下面的代码是一个文件夹中的所有文件重新命名并改变大小
from PIL import Image
from PIL import ImageTk
import os
path1 = '/home/python/Desktop/zz/bad/'
path2 = '/home/python/Desktop/zz/input_data_resized/'
#path2 = './input_data/'
pictures = []
for root, dirs, files in os.walk(path1):
for f in files:
pictures.append(os.path.join(root,f))
src = "0"
i=0
lenth= len(src)
print(lenth)
for dir in pictures:
#duqu meizhang tupian
im = Image.open(dir)
#shengcheng 000001 000002 ...
s = src[0:(lenth-len(str(i)))] + str(i+2950)
width,high = im.size
#chongxin shezhi tupian daxiao
if width > high:
out = im.resize((200, 256), Image.ANTIALIAS)
else:
out = im.resize((200, 256), Image.ANTIALIAS)
out.save(path2+s+'.jpeg')
i = i+1