[Python3]批量修改图片尺寸

2023-05-16

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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

[Python3]批量修改图片尺寸 的相关文章

随机推荐