小編給大家分享一下Python3.4中tkinter,PIL圖片轉(zhuǎn)換的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),西疇網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:西疇等地區(qū)。西疇做網(wǎng)站價(jià)格咨詢:18980820575先給大家分享一下全部代碼
import os from PIL import Image import tkinter import tkinter.filedialog import tkinter.messagebox class Window(): def __init__(self): self.root = root = tkinter.Tk() self.menu = tkinter.Menu(root) self.submenu = tkinter.Menu(self.menu, tearoff=0) self.submenu.add_command(label='作者: 王小濤同學(xué)') root.config(menu=self.submenu) self.Image = tkinter.StringVar() self.Image.set('.bmp') self.mstatus = tkinter.IntVar() self.fstatus = tkinter.IntVar() self.mstatus.set(0) self.fstatus.set(0) self.status = tkinter.StringVar() self.label = tkinter.Label(root, text='輸入百分比') self.label.place(x=5, y=5) self.entryNew = tkinter.Entry(root) self.entryNew.place(x=70, y=5) self.checkM = tkinter.Checkbutton(self.root, text='批量轉(zhuǎn)換', command=self.OnCheckM, variable=self.mstatus, onvalue=1, offvalue=0) self.checkM.place(x=5, y=30) self.label = tkinter.Label(root, text='選擇文件') self.label.place(x=5, y=55) self.entryFile = tkinter.Entry(root) self.entryFile.place(x=70, y=55) self.BrowserFileButton = tkinter.Button(root, text='瀏覽', command=self.BrowserFile) self.BrowserFileButton.place(x=220, y=55) self.label = tkinter.Label(root, text='選擇目錄') self.label.place(x=5, y=90) self.entryDir = tkinter.Entry(root, state=tkinter.DISABLED) self.entryDir.place(x=70, y=90) self.BrowserDirButton = tkinter.Button(root, text='瀏覽', command=self.BrowserDir, state=tkinter.DISABLED) self.BrowserDirButton.place(x=220, y=90) self.checkF = tkinter.Checkbutton(root, text='改變文件格式', onvalue=1, offvalue=0, variable=self.fstatus, command=self.OnCheckF) self.checkF.place(x=5, y=120) frame = tkinter.Frame(root, ) frame.place(x=10, y=150) self.rBmp = tkinter.Radiobutton(frame, variable=self.Image, value='.bmp', text='BMP', state=tkinter.DISABLED) self.rBmp.pack(anchor='w') self.rJpg = tkinter.Radiobutton(frame, variable=self.Image, value='.jpg', text='JPG', state=tkinter.DISABLED) self.rJpg.pack(anchor='w') self.rPng = tkinter.Radiobutton(frame, variable=self.Image, value='.png', text='PNG', state=tkinter.DISABLED) self.rPng.pack(anchor='w') self.rGif = tkinter.Radiobutton(frame, variable=self.Image, value='.gif', text='GIF', state=tkinter.DISABLED) self.rGif.pack(anchor='w') self.ButtonCov = tkinter.Button(root, text='轉(zhuǎn)換格式', command=self.Conv, ) self.ButtonCov.place(x=120, y=180) self.statusLabel = tkinter.Label(root, textvariable=self.status, fg='red') self.statusLabel.place(x=80, y=220) def OnCheckM(self): if not self.mstatus.get(): self.entryDir.config(state=tkinter.DISABLED) self.entryFile.config(state=tkinter.NORMAL) self.BrowserFileButton.config(state=tkinter.NORMAL) self.BrowserDirButton.config(state=tkinter.DISABLED) else: self.entryDir.config(state=tkinter.NORMAL) self.entryFile.config(state=tkinter.DISABLED) self.BrowserFileButton.config(state=tkinter.DISABLED) self.BrowserDirButton.config(state=tkinter.NORMAL) def OnCheckF(self): if not self.fstatus.get(): self.rBmp.config(state=tkinter.DISABLED) self.rPng.config(state=tkinter.DISABLED) self.rJpg.config(state=tkinter.DISABLED) self.rGif.config(state=tkinter.DISABLED) else: self.rBmp.config(state=tkinter.NORMAL) self.rPng.config(state=tkinter.NORMAL) self.rJpg.config(state=tkinter.NORMAL) self.rGif.config(state=tkinter.NORMAL) def BrowserFile(self): file = tkinter.filedialog.askopenfilename(title='Python player', filetypes=[('JPG', '*.jpg'), ('BMP', '*.bmp'), ('GIF', '*.gif'), ('PNG', '*.png')]) if file: self.entryFile.delete(0, tkinter.END) self.entryFile.insert(tkinter.END, file) def BrowserDir(self): directory = tkinter.filedialog.askdirectory(title='Python') if directory: self.entryDir.delete(0, tkinter.END) self.entryDir.insert(tkinter.END, directory) def make(self, file, format=None): im = Image.open(file) mode = im.mode if mode not in('L', 'RGB'): im = im.convert('RGB') width, height = im.size s = self.entryNew.get() if s == '': tkinter.messagebox.showerror('出錯(cuò)啦', '請(qǐng)輸入百分比') return else: n = int(s) nwidth = int(width*n/100) nheight = int(height*n/100) thumb = im.resize((nwidth, nheight), Image.ANTIALIAS) if format: thumb.save(file[:(len(file)-4)] + '_thumb' + format) else: thumb.save(file[:(len(file)-4)] + '_thumb' + file[-4:]) def Conv(self): n = 0 if self.mstatus.get(): path = self.entryDir.get() if path == "": tkinter.messagebox.showerror('出錯(cuò)啦', '請(qǐng)選擇路徑') return filenames = os.listdir(path) if self.fstatus.get(): f = self.Image.get() print(f) for filename in filenames: if filename[-3:] in ('bmp', 'jpg', 'gif', 'png'): self.make(path+'/'+filename, f) n += 1 else: for filename in filenames: if filename[-3:] in ('bmp', 'jpg', 'gif', 'png'): self.make(path+'/'+filename) n += 1 else: file = self.entryFile.get() if file == '': tkinter.messagebox.showerror('出錯(cuò)啦', '請(qǐng)選擇文件') return if self.fstatus.get(): f = self.Image.get() self.make(file, f) n += 1 else: self.make(file) n += 1 self.status.set('成功轉(zhuǎn)換 %d 張圖片' % n) def mainloop(self): self.root.minsize(280, 270) self.root.maxsize(280, 250) self.root.title('圖片轉(zhuǎn)換') self.root.mainloop() if __name__ == "__main__": window = Window() window.mainloop()
運(yùn)行后的實(shí)際效果如下
看完了這篇文章,相信你對(duì)“Python3.4中tkinter,PIL圖片轉(zhuǎn)換的示例分析”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
分享標(biāo)題:Python3.4中tkinter,PIL圖片轉(zhuǎn)換的示例分析-創(chuàng)新互聯(lián)
瀏覽地址:http://jinyejixie.com/article48/jgiep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、服務(wù)器托管、App開發(fā)、網(wǎng)站維護(hù)、移動(dòng)網(wǎng)站建設(shè)、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容