脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python+pyqt5实现图片批量缩放工具

python+pyqt5实现图片批量缩放工具

2021-06-07 00:13daxuewei Python

这篇文章主要为大家详细介绍了Python+pyqt5实现图片批量缩放工具,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

批量修改图片大小好像用ps也可以,不过我不会,程序猿就用程序来解决。
这段时间学了下python,很强大,之前一些不知道怎么处理的东西在python里面都能找到解决方法。

工具界面如下图

python+pyqt5实现图片批量缩放工具

这个工具需要用到第三方库 pillow 和 pyqt5,可通过命令行安装。

?
1
2
pip install pillow
pip install pyqt5

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!-*-coding:utf-8-*-
 
from pil import image
 
import hashlib, os, sys
from pyqt5.qtgui import qfont, qicon
from pyqt5.qtwidgets import (qfiledialog, qmessagebox,
  qgridlayout, qlineedit, qtextedit, qlabel, qwidget, qtooltip,
  qpushbutton, qapplication, qinputdialog)
from pyqt5.qtcore import qcoreapplication
 
 
def makedir(path):
  if not os.path.exists(path):
    os.mkdir(path)
 
 
def image_resize(image_path, scale):
  #获得新文件名称
  out_path = os.path.splitext(image_path)[0] + "_after" + os.path.splitext(image_path)[-1]
  with image.open( image_path ) as im:
    width = int(im.size[0]*scale)
    height = int(im.size[1]*scale)
    #resize 设置新尺寸
    out = im.resize( (width, height) )
    out.save(out_path)
 
def folder_resize(folder_path, scale):
  dst_files = []
  files = os.listdir( folder_path )
  for file in files:
    ext = os.path.splitext(file)[-1]
    #只支持jpg或png文件
    if ext == ".jpg" or ext == ".png":
      dst_files.append(file)
 
  #输出路径
  out_folder = os.path.join(folder_path, "after")
  makedir(out_folder)
 
  for item in dst_files:
    try:
      op = os.path.join( out_folder, item )
      with image.open( os.path.join(folder_path, item) ) as im:
        width = int(im.size[0]*scale)
        height = int(im.size[1]*scale)
        out = im.resize( (width, height) )
        out.save(op)
    except filenotfounderror:
      print("file not found " + item)
 
 
class mainbody(qwidget):
  def __init__(self):
    super(mainbody, self).__init__()
    self.init()
 
    self.m_scale = 1.0
    self.m_width = 0
    self.m_height = 0
 
  def init(self):
    grid = qgridlayout()
    self.setlayout(grid)
    grid.setspacing(10)
 
    self.m_tedit = qtextedit()
    self.m_tedit.settooltip( u'可以拖拽文件到这里来' )
    grid.addwidget(self.m_tedit, 1, 0, 4, 3)
 
    self.m_scale_set_btn = qpushbutton( u"设置缩放值" )
    self.m_scale_set_btn.clicked.connect( self.set_scale_func )
    grid.addwidget( self.m_scale_set_btn, 1, 3 )
    self.m_scale_label = qlabel( "1.0" )
    grid.addwidget( self.m_scale_label, 2, 3 )
 
    self.m_height_set_btn = qpushbutton( u"设置高度" )
    self.m_height_set_btn.clicked.connect( self.set_height_func )
    grid.addwidget( self.m_height_set_btn, 3, 3 )
    self.m_height_label = qlabel( "0" )
    grid.addwidget( self.m_height_label, 4, 3 )
 
    self.m_width_set_btn = qpushbutton( u"设置宽度" )
    self.m_width_set_btn.clicked.connect( self.set_width_func )
    grid.addwidget( self.m_width_set_btn, 5, 3 )
    self.m_width_label = qlabel( "0" )
    grid.addwidget( self.m_width_label, 6, 3 )
 
    self.m_file_btn = qpushbutton(u'选择文件')
    self.m_file_btn.clicked.connect( self.file_func )
 
    self.m_folder_btn = qpushbutton(u'选择文件夹')
    self.m_folder_btn.clicked.connect( self.folder_func )
 
    self.m_generate_btn = qpushbutton(u'处理')
    self.m_generate_btn.clicked.connect( self.generate_func )
 
    self.m_clear_btn = qpushbutton(u'清空')
    self.m_clear_btn.clicked.connect( self.clear_func )
 
    grid.addwidget(self.m_file_btn, 5, 0)
    grid.addwidget(self.m_folder_btn, 5, 1)
    grid.addwidget(self.m_generate_btn, 6, 0)
    grid.addwidget(self.m_clear_btn, 6, 1)
 
    self.setgeometry(300,300,300,200)
    self.setwindowtitle(u"批量图片缩放")
    self.setwindowicon(qicon('icon.png'))
    self.show()
 
  #按照输入的缩放值进行缩放
  def set_scale_func(self):
    text, ok = qinputdialog.gettext( self, 'input scale', u'输入缩放值' )
    if ok:
      try:
        scale = float( text )
      except valueerror:
        qmessagebox.warning( self, u"设置缩放值", u"输入错误" )
      else:
        self.m_scale = scale
        self.refresh_setting(0)
 
  #根据高度的缩放比例等比例缩放
  def set_height_func(self):
    text, ok = qinputdialog.gettext( self, 'input height', u'输入高度' )
    if ok:
      try:
        height = int( text )
      except valueerror:
        qmessagebox.warning( self, u"设置高度", u"输入错误" )
      else:
        self.m_height = height
        self.refresh_setting(2)
 
  #根据宽度的缩放比例等比例缩放
  def set_width_func(self):
    text, ok = qinputdialog.gettext( self, 'input width', u'输入宽度' )
    if ok:
      try:
        width = int( text )
      except valueerror:
        qmessagebox.warning( self, u"设置宽度", u"输入错误" )
      else:
        self.m_width = width
        self.refresh_setting(1)
 
  def file_func(self):
    filename, ok = qfiledialog.getopenfilename(self, "open file", "c:/users/administrator/desktop", "jpg (*.jpg);;png (*.png)")
    if ok:       
      self.m_tedit.settext( filename )
 
      size = self.get_image_size( filename )
      s,w,h = self.get_current_setting()
      self.m_width = int(size[0]*s)
      self.m_height = int(size[1]*s)
      self.m_height_label.settext( str( self.m_height ) )
      self.m_width_label.settext( str( self.m_width ) )
 
  def folder_func(self):
    filename = qfiledialog.getexistingdirectory(self, "open file", "c:/users/administrator/desktop")
    self.m_tedit.settext( filename )
 
 
  def generate_func(self):
    path = self.get_image_path()
    if path == none or path == "":
      qmessagebox.warning( self, u"error", u"未选择文件" )
      return
    elif os.path.exists(path) == false:
      qmessagebox.warning( self, u"error", u"文件不存在" )
      return
 
    if os.path.isdir(path):
      folder_resize( path, self.m_scale )
    elif os.path.isfile(path):
      image_resize( path, self.m_scale )
 
 
  def clear_func(self):
    self.m_tedit.clear()
    self.m_width = 0
    self.m_height = 0
    self.m_width_label.settext("0")
    self.m_height_label.settext("0")
 
  def get_image_size(self, image_path):
    with image.open(image_path) as im:
      return im.size
 
  def get_image_path(self):
    path = self.m_tedit.toplaintext()
    if path.startswith( 'file:///' ):
      path = path[8:]
    return path
 
  def get_current_setting(self):
    return self.m_scale, self.m_width, self.m_height
 
  def refresh_setting(self, kind):
    s, w, h = self.get_current_setting()
    p = self.get_image_path()
    if kind == 0:
      if p == none or p == "" or os.path.exists(p) == false or os.path.isdir(p) == true:
        w = 0
        h = 0
      else:
        size = self.get_image_size(p)
        w = size[0]*s
        h = size[1]*s
 
    elif kind == 1:
      if p == none or p == "" or os.path.exists(p) == false:
        qmessagebox.warning( self, u"warning", u"请先选择文件" )
        return
      elif os.path.isdir(p) == true:
        qmessagebox.warning( self, u"warning", u"文件夹不能设置宽度" )
        return
 
      size = self.get_image_size(p)
      w = int(w)
      s = w*1.0/size[0]
      h = int((w/size[0])*size[1])
 
    elif kind == 2:
      if p == none or p == "" or os.path.exists(p) == false:
        qmessagebox.warning( self, u"warning", u"请先选择文件" )       
        return
      elif os.path.isdir(p) == true:
        qmessagebox.warning( self, u"warning", u"文件夹不能设置高度" )
        return
 
      size = self.get_image_size(p)
      h = int(h)
      s = h*1.0/size[1]
      w = int((h/size[1])*size[0])
 
    self.m_width = int(w)
    self.m_height = int(h)
    self.m_scale = float(s)
    self.m_width_label.settext(str(self.m_width))
    self.m_height_label.settext(str(self.m_height))
    self.m_scale_label.settext(str(self.m_scale))
 
 
 
if __name__ == "__main__":
  app = qapplication(sys.argv)
  mb = mainbody()
  sys.exit( app.exec_() )

整个工具比较简陋,能用就好。有什么不正确的地方请指出。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u012755940/article/details/78292163

延伸 · 阅读

精彩推荐