效率提高的問題python
以前朋友須要把大量的圖片用分辨率進行區分查找,他說都是打開圖片,而後用尺子在屏幕上量。。。。。。我也是瀑布汗。。。。花的點時間幫他寫的小軟件,解決這個蛋疼的問題app
解決方案工具
本想用批處理解決,可是考慮到易用性,就用python的tkinter作了簡單的界面方便操做。oop
他也不是程序開發人員,讓他安裝python環境並不現實,就須要用打包工具處理,網上看到不少用py2exe,看起來有點麻煩,我就直接用pyinstaller打包了,一行代碼搞定。spa
源代碼code
1 # -*- coding: utf-8 -*- 2 import os 3 from PIL import Image as pilImage 4 from tkinter import * 5 import tkinter.messagebox as messagebox 6 import tkinter.filedialog as dialog 7 8 class Application(Frame): 9 def __init__(self, master=None): 10 Frame.__init__(self, master) 11 self.pack() 12 self.createWidgets() 13 14 def createWidgets(self): 15 Label(self, text="輸入地址:", font=("微軟雅黑", 12), width=10, height=1).grid(row=0) 16 Label(self, text="輸出地址:", font=("微軟雅黑", 12), width=10, height=1).grid(row=1) 17 Label(self, text="長寬比:", font=("微軟雅黑", 12), width=10, height=1).grid(row=2) 18 self.inInput = Entry(self) 19 self.outInput = Entry(self) 20 self.minInput = Entry(self,width=8) 21 Label(self, text="-", font=("微軟雅黑", 12), width=1, height=1).grid(row=2,column=2) 22 self.maxInput = Entry(self,width=8) 23 self.inInput.grid(row=0,column=1,columnspan=3) 24 self.outInput.grid(row=1,column=1,columnspan=3) 25 self.minInput.grid(row=2,column=1) 26 self.maxInput.grid(row=2,column=3) 27 28 self.minInput.insert(END,1) 29 self.maxInput.insert(END,1.1) 30 31 self.inButton = Button(self, text='選擇', command=self.openInDir) 32 self.outButton = Button(self, text='選擇', command=self.openOutDir) 33 self.inButton.grid(row=0,column=5) 34 self.outButton.grid(row=1,column=5) 35 36 self.excuteButton = Button(self, text='輸出', command=self.export) 37 self.excuteButton.grid(row=2,column=5) 38 39 def export(self): 40 in_path = self.inInput.get() 41 out_path = self.outInput.get() 42 excute_path = '' 43 excute_count = 0 44 files = os.listdir(in_path) 45 for file in files: 46 excute_path = in_path + '/' + file 47 im = pilImage.open(excute_path,'r') 48 if im.size[1]/im.size[0] >= float(self.minInput.get()) and im.size[1]/im.size[0] <= float(self.maxInput.get()): 49 im.save(out_path + '/' + file, "PNG") 50 print(out_path + '/' + file) 51 excute_count = excute_count + 1 52 messagebox.showinfo('Message', excute_count) 53 54 def openInDir(self): 55 self.inInput.delete(0,END) 56 self.inInput.insert(END,dialog.askdirectory()) 57 58 def openOutDir(self): 59 self.outInput.delete(0,END) 60 self.outInput.insert(END,dialog.askdirectory()) 61 62 app = Application() 63 app.master.title('圖片處理') 64 app.mainloop()
其餘相關blog
這裏有直接打包好的exe問題 -----> 下載地址圖片
運行截圖:utf-8