700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Py_GUI:证件照压缩处理工具

Py_GUI:证件照压缩处理工具

时间:2020-02-23 18:03:44

相关推荐

Py_GUI:证件照压缩处理工具

需求

应对各类报名证件照要求和其他照片需求

PS裁切专业性高且极度压缩后模糊

应对欧玛软件限制背景,无法自定义裁切长宽

功能

自由裁剪长宽比例,滑动照片质量进行照片大小的压缩

界面

原图

裁切后:

原理

PIL库

代码

库:

import osfrom threading import Threadfrom tkinter import StringVar,Tk,Label,Entry,Button,Scale,HORIZONTAL,LEFT,NORMAL,SUNKEN,NWfrom tkinter import messageboxfrom tkinter.filedialog import askdirectory, askopenfilenamefrom PIL import Image as igfrom PIL import ImageTkig.MAX_IMAGE_PIXELS = None

函数:

def selectPath():'''选择路径'''dicpath = askopenfilename()if dicpath:path.set(dicpath)img_open = ig.open(dicpath)img_open=img_open.resize((295, 413))img_png = ImageTk.PhotoImage(img_open)img_label.config(image=img_png)img_label.img=img_pngelse:return Nonedef resize_img(pic,l,k,quality):'''裁切照片pic:照片路径l:长k:宽quality:照片质量'''img=ig.open(pic)resized_img=img.resize((l,k))newfile=pic.split(".")[0]+'_resized.jpg'out_pic_path.set(newfile)resized_img.save(newfile,quality=quality)size=str((os.stat(newfile).st_size)/1000)[:3]+'KB'success.set('存放位置:\n'+f'{newfile}'+'\n'+'点击【打开保存位置】查看图片'+'\n'+f'裁切后的照片大小为【{size}】')def resizeDef():'''裁切button函数'''try:pic=path.get()l=pic_length.get()k=pic_width.get()if l=='' or k =='':l,k=295,413quality=qual.get()print(quality)resize_img(pic,int(l),int(k),int(quality))except:r = messagebox.askokcancel('警告', '请选择图片')def openSuccessdir():'''打开生成后的图片文件夹'''try:dirs=out_pic_path.get()if dirs=='':r = messagebox.askokcancel('警告', '请裁切后重试!')else:os.startfile(os.path.dirname(dirs))except:r = messagebox.askokcancel('警告', '请裁切后重试!')def openPic():'''查看裁切图片'''url=out_pic_path.get()if url=='':r = messagebox.askokcancel('警告', '您还未选择照片或者为裁切!')else:os.startfile(url)#线程启动,防止tk卡死----------------------------------------def thread_run():if path.get()!='':success.set('正在裁切-----请勿重复点击裁切按钮!')t1=Thread(target=resizeDef)t1.start()else:r = messagebox.askokcancel('警告', '请选择图片')def thread_open():t2=Thread(target=selectPath)t2.start()#-------------------------------------------------------

Tk布局:

app=Tk()app.title('证件照压缩裁切工具')app.geometry("810x420+300+300")path = StringVar()#当前路径显示success=StringVar()#正在处理的文件名pic_length=StringVar()#自定义长pic_width=StringVar()#自定义宽qual=StringVar()#图片质量,即压缩比例dicpath=''#当前路径out_pic_path=StringVar()x_entry=110#裁切长元素Label(app,text = "裁切宽(像素):",font='20px').place(x=1,y=40)Entry(app,textvariable=pic_length,width='50').place(x=x_entry,y=40)#长(横着)#裁切宽元素Label(app,text = "裁切长(像素):",font='20px').place(x=1,y=80)Entry(app,textvariable=pic_width,width='50').place(x=x_entry,y=80)#宽#滑动条元素Label(app,text = "照片质量",font='20px').place(x=1,y=120)Scale(app,from_=0,to=100,tickinterval=10,orient=HORIZONTAL,resolution=10,length=350,variable=qual).place(x=x_entry,y=120)#裁切成功提示语Label(app,textvariable=success,font='5px',bg='yellow',justify=LEFT).place(x=10,y=230)#选择图片Button(app, text = "选择照片", command = thread_open,font='30px').place(x=5,y=7)Entry(app, textvariable = path,width='50').place(x=110,y=7)#路径#button功能区Button(app, text = "裁切照片",font='30px',command=thread_run,state=NORMAL).place(x=85,y=190)Button(app, text = "查看裁切后的照片",font='30px',command=openPic).place(x=160,y=190)Button(app, text = "打开保存位置",font='30px',command=openSuccessdir).place(x=300,y=190)#readmeinfo='软件说明:\n\默认为295x413-图片质量为0[清晰度和文件大小受质量影响]\n\对于大照片裁切可能有卡顿请耐心等待\n\对于照片指定KB以下压缩,请根据结果滑动质量滑块多次尝试\n\20KB一下建议照片质量为70\n\作者:liubingzhe'Label(app,text=info,font='10px',justify=LEFT).place(x=5,y=300)img_label = Label(app,relief=SUNKEN,width=295,height=413,text='照片预览区域\n大照片加载可能缓慢,请耐心等待\n请等待照片加载完成再进行裁切\n横着为宽,竖着为长',justify=LEFT,anchor=NW)img_label.place(x=500,y=0)app.mainloop()

下载

/download/qq_44198436/12663627

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。