zoukankan      html  css  js  c++  java
  • 按照需要分别率长宽比导出图片(python 3)

    效率提升的问题

    之前朋友需要把大量的图片用分辨率进行区分查找,他说都是打开图片,然后用尺子在屏幕上量。。。。。。我也是瀑布汗。。。。花的点时间帮他写的小软件,解决这个蛋疼的问题

    解决方案

    本想用批处理解决,但是考虑到易用性,就用python的tkinter做了简单的界面方便操作。

    他也不是程序开发人员,让他安装python环境并不现实,就需要用打包工具处理,网上看到很多用py2exe,看起来有点麻烦,我就直接用pyinstaller打包了,一行代码搞定。

    源代码

     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()

    其他相关

    这里有直接打包好的exe问题 ----->  下载地址

    运行截图:

  • 相关阅读:
    转dhdhtmlxTree
    转Merge的用法
    解决SqlServer2008评估期过期
    借鉴一下对比算法
    Asp.Net异常:"由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值"的解决方法
    查看Windows下引起Oracle CPU占用率高的神器-qslice
    今日有奖活动一览
    【分享】给做技术的战友们推荐一个不错的微信公号解解闷
    Unreal Engine Plugin management
    当在ECLIPSE中import现存项目时,如遇到版本不符
  • 原文地址:https://www.cnblogs.com/nightcat/p/5363430.html
Copyright © 2011-2022 走看看