|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位老师,大家晚上好!我写了一个小脚本用来批量把图片的像素尺寸缩小到原来的一半,利用tkinter设计了一个简单的交互界面,因为是批量处理,我想设计一个进度条用来跟踪循环体的进度,同时希望完成之后出来一个弹出窗口告诉用户已经处理完成,因为刚刚接触Python,可否请各位老师帮忙一下,非常感谢!
import os
from PIL import Image, ImageEnhance
import PIL
import tkinter
import tkinter.filedialog
import tkinter.messagebox
class Window():
def __init__(self):
self.root = root = tkinter.Tk()
self.label = tkinter.Label(root, text='选择目录')
self.label.grid(row=3, column=0)
self.entryDir = tkinter.Entry(root)
self.entryDir.grid(row=3, column=1)
self.BrowserDirButton = tkinter.Button(root, text='浏览', command=self.BrowserDir)
self.BrowserDirButton.grid(row=3, column=2)
self.ButtonCov = tkinter.Button(root, text='开始处理', command=self.Conv, )
self.ButtonCov.grid(row=4, column=0)
def BrowserDir(self):
directory = tkinter.filedialog.askdirectory(title='Python')
if directory:
self.entryDir.delete(0, tkinter.END)
self.entryDir.insert(tkinter.END, directory)
def Conv(self):
path = self.entryDir.get()
for file in os.listdir(path):
if file[-4:] in ('.bmp', '.jpg', 'jpeg','.gif', '.png', '.BMP', '.JPG', 'JPGE','.GIF', '.PNG'):
f_img = path + "/" + file
image = Image.open(f_img)
newwidth = int(image.size[0] / 2)
newheight = int(image.size[1] / 2)
image = image.resize((newwidth, newheight), PIL.Image.NEAREST)
image.save(f_img)
def mainloop(self):
self.root.minsize(330, 190)
self.root.maxsize(330, 170)
self.root.title('图片批量处理器')
self.root.mainloop()
if __name__ == "__main__":
window = Window()
window.mainloop()
import os
from PIL import Image, ImageEnhance
import PIL
import tkinter
import tkinter.filedialog
import tkinter.messagebox
from tkinter.ttk import Progressbar
from time import sleep
from threading import Thread
class Window():
def __init__(self):
self.root = root = tkinter.Tk()
self.label = tkinter.Label(root, text='选择目录')
self.label.grid(row=3, column=0)
self.entryDir = tkinter.Entry(root)
self.entryDir.grid(row=3, column=1)
self.BrowserDirButton = tkinter.Button(root, text='浏览', command=self.BrowserDir)
self.BrowserDirButton.grid(row=3, column=2)
self.ButtonCov = tkinter.Button(root, text='开始处理', command=self.Conv, )
self.ButtonCov.grid(row=4, column=0)
def BrowserDir(self):
directory = tkinter.filedialog.askdirectory(title='Python')
if directory:
self.entryDir.delete(0, tkinter.END)
self.entryDir.insert(tkinter.END, directory)
def Conv(self):
path = self.entryDir.get()
def _prog():
top = tkinter.Toplevel(self.root)
top.attributes("-toolwindow", 1)
top.attributes("-topmost", 1)
top.title("进度条")
tkinter.Label(top, text="正在处理图片,请稍候…").pack()
prog = Progressbar(top, mode="indeterminate")
prog.pack()
prog.start()
for file in os.listdir(path):
if file[-4:] in ('.bmp', '.jpg', 'jpeg','.gif', '.png', '.BMP', '.JPG', 'JPGE','.GIF', '.PNG'):
f_img = path + "/" + file
image = Image.open(f_img)
newwidth = int(image.size[0] / 2)
newheight = int(image.size[1] / 2)
image = image.resize((newwidth, newheight), PIL.Image.NEAREST)
image.save(f_img)
sleep(0.5)
prog.stop()
top.destroy()
tkinter.messagebox.showinfo("提示:", "已完成!")
t = Thread(target=_prog)
t.start()
def mainloop(self):
self.root.minsize(330, 190)
self.root.maxsize(330, 170)
self.root.title('图片批量处理器')
self.root.mainloop()
if __name__ == "__main__":
window = Window()
window.mainloop()
|
|