[Python3]批量修改图片尺寸

from PIL import ImageColor as ic
from PIL import Image
import os,sys
def resizeImage(imgName,maxSize,newName): # 参数1:图片名称,参数2:修改后图片最长边的像素个数
	image = Image.open(imgName)
	width,height = image.size
	nwidth,nheight = maxSize,int(height * maxSize / width)
	if width < height: nheight,nwidth = maxSize,int(width * maxSize / height)
	nimage = image.resize((nwidth,nheight))
	imgSecName = imgName[imgName.rfind("."):] # 后缀名
	nimage.save(newName + imgSecName)

def main():
	imgList,imgList2 = os.listdir(),[]
	imgList.remove(sys.argv[0]) # 获取当前目录中所有图片的名称
	for it in imgList: imgList2.append((it,os.path.getmtime(it)))
	imgList2.sort(key=lambda x:x[1]) # 按图片修改时间排序
	for idx,it in enumerate(imgList2): resizeImage(it[0],4000,str(idx+1) + "2")
if __name__ == '__main__':
	main()

你可能感兴趣的:(python,python,开发语言)