DOS版
from PIL import Image import os print(‘使用说明:\n‘) print(‘1、寒江独钓作品,专为处理学生学籍照片而设计。\n‘) print(‘2、要求系统为64位操作系统。\n‘) print(‘3、如运行出错请先安装vc_redist.x64.exe(c++2015运行库)。\n‘) print(‘4、右键点击标题,在编辑中进行粘贴所复制的路径。\n‘) print(‘----------------------------------------‘) print(‘开始操作:\n‘) filePath=‘‘ savePath=‘‘ file=[] filePath = input(‘请输入要处理的图片文件夹:‘) savePath = input(‘请输入要要存放处理后的文件夹:‘) savePath+=r‘/‘ yesno = input(‘确定开始转换吗?(y/n)‘) if yesno == ‘y‘: print(‘处理中,请稍等...‘) for root,dirs,files in os.walk(filePath): for name in files: t = root+r‘/‘+name file.append(t) for i in file: t1,t2 = os.path.split(i) print(‘正在处理:‘,i) im = Image.open(i) im.thumbnail((240, 320), Image.ANTIALIAS) im.save(savePath+t2.split(‘.‘)[0]+‘_cyp‘+‘.jpg‘) im.close() print(‘转换完成!‘) else: print(‘中止转换!‘) input(‘按任意键退出!‘)
界面版
im.py
from PIL import Image from pyCyp import fun import os from tkinter import * from tkinter import messagebox win = Tk() win.title(‘神速批量修改图片大小--寒江独钓作品‘) win.geometry(‘600x340+400+200‘) win.resizable(width=False,height=False) group2 = LabelFrame(win,font=(None ,10),padx=5,pady=5) group2.grid(row=0,column = 0,columnspan=9,padx=10,pady=10) varName = StringVar() varName.set(‘‘) name = Entry(group2,width=58,textvariable = varName) name.grid(row=0,column = 0,padx=10,pady=10) group1 = LabelFrame(win,text=‘具体设置‘,font=(None ,10),padx=5,pady=5) group1.grid(row=1,column = 0,columnspan=9,padx=10,pady=10) Label(group1,text = ‘图片长度:‘,font=(None ,12)).grid(row=0,column = 0,padx=10,pady=15) varchang = StringVar() buyNum = Entry(group1,width=10,textvariable = varchang) buyNum.grid(row=0,column = 1,padx=10,pady=10) varchang.set(‘240‘) Label(group1,text = ‘图片宽度:‘,font=(None ,12)).grid(row=0,column = 2,padx=10,pady=15) varkuan = StringVar() buyNum = Entry(group1,width=10,textvariable = varkuan) buyNum.grid(row=0,column = 3,padx=10,pady=10) varkuan.set(‘320‘) Label(group1,text = ‘分辨率:‘,font=(None ,12)).grid(row=0,column = 4,padx=10,pady=15) varfen = StringVar() buyNum = Entry(group1,width=10,textvariable = varfen,state=‘disabled‘) buyNum.grid(row=0,column = 5,padx=10,pady=10) varfen.set(‘Thanks‘) varName1 = StringVar() varName1.set(‘‘) name1 = Entry(win,width=60,textvariable = varName1) name1.grid(row=2,column = 0,padx=10,pady=10) group3 = LabelFrame(win,width=80,font=(None ,10),padx=5,pady=5) group3.grid(row=3,column = 0,columnspan=9,padx=10,pady=1) filePath=‘‘ savePath=‘‘ fileName = [] def xiugai(img,x,y): t1,t2 = os.path.split(img) im = Image.open(img) im.thumbnail((x, y), Image.ANTIALIAS) im.save(savePath+t2.split(‘.‘)[0]+‘_cyp‘+‘.jpg‘) im.close() def selectPhoto(): global filePath fileDir = filedialog.askdirectory() filePath=fileDir varName.set(filePath) def savePhoto(): global savePath fileDir = filedialog.askdirectory() savePath=fileDir+‘/‘ varName1.set(savePath) def start(): fun(filePath,savePath) messagebox.showinfo(‘提示‘,‘转换完成!‘) def exitWin(): win.destroy() Button(group2,text=‘选择图片文件夹‘,width = 15,command=selectPhoto).grid(row=0,column = 2,padx=10,pady=10) Button(win,text=‘选择保存位置‘,width = 15,command=savePhoto).grid(row=2,column = 2,padx=10,pady=10) Button(group3,text=‘开始处理‘,width = 30,command=start).grid(columnspan=2,row=0,column = 0,padx=10,pady=10) Button(group3,text=‘退出程序‘,width = 30,command=exitWin).grid(columnspan=3,row=0,column = 2,padx=10,pady=10) mainloop()
pyCyp.py
from PIL import Image import os def fun(filePath,savePath): file=[] for root,dirs,files in os.walk(filePath): for name in files: t = root+r‘/‘+name file.append(t) for i in file: t1,t2 = os.path.split(i) im = Image.open(i) im.thumbnail((240, 320), Image.ANTIALIAS) im.save(savePath+t2.split(‘.‘)[0]+‘_cyp‘+‘.jpg‘) im.close()
时间: 2024-10-22 12:06:55