基于pyqt5的图片素材批量处理工具

  • 功能

    分辨率的批量转换,文件夹递归查找

    像素偏移量批量调整,文件夹单层查找

    画布的大小的批量进行调整,不进行缩放,文件夹单层查找

  • 界面

  • 通过PyUIC生成的代码
  1 # -*- coding: utf-8 -*-
  2 
  3 # Form implementation generated from reading ui file 'E:\Development\IProcesser/IProcessingDialog.ui'
  4 #
  5 # Created by: PyQt5 UI code generator 5.5.1
  6 #
  7 # WARNING! All changes made in this file will be lost!
  8 
  9 from PyQt5 import QtCore, QtGui, QtWidgets
 10 
 11 class Ui_Dialog(object):
 12     def setupUi(self, Dialog):
 13         Dialog.setObjectName("Dialog")
 14         Dialog.resize(411, 525)
 15         self.verticalLayout_2 = QtWidgets.QVBoxLayout(Dialog)
 16         self.verticalLayout_2.setObjectName("verticalLayout_2")
 17         self.tabWidget = QtWidgets.QTabWidget(Dialog)
 18         self.tabWidget.setObjectName("tabWidget")
 19         self.tab = QtWidgets.QWidget()
 20         self.tab.setObjectName("tab")
 21         self.verticalLayout = QtWidgets.QVBoxLayout(self.tab)
 22         self.verticalLayout.setObjectName("verticalLayout")
 23         self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
 24         self.horizontalLayout_5.setObjectName("horizontalLayout_5")
 25         self.label_5 = QtWidgets.QLabel(self.tab)
 26         self.label_5.setObjectName("label_5")
 27         self.horizontalLayout_5.addWidget(self.label_5)
 28         self.reslotion_src_le = QtWidgets.QLineEdit(self.tab)
 29         self.reslotion_src_le.setObjectName("reslotion_src_le")
 30         self.horizontalLayout_5.addWidget(self.reslotion_src_le)
 31         self.reslotion_src_view_btn = QtWidgets.QPushButton(self.tab)
 32         self.reslotion_src_view_btn.setObjectName("reslotion_src_view_btn")
 33         self.horizontalLayout_5.addWidget(self.reslotion_src_view_btn)
 34         self.verticalLayout.addLayout(self.horizontalLayout_5)
 35         self.horizontalLayout_7 = QtWidgets.QHBoxLayout()
 36         self.horizontalLayout_7.setObjectName("horizontalLayout_7")
 37         self.label_8 = QtWidgets.QLabel(self.tab)
 38         self.label_8.setObjectName("label_8")
 39         self.horizontalLayout_7.addWidget(self.label_8)
 40         self.reslotion_dst_le = QtWidgets.QLineEdit(self.tab)
 41         self.reslotion_dst_le.setEnabled(True)
 42         self.reslotion_dst_le.setObjectName("reslotion_dst_le")
 43         self.horizontalLayout_7.addWidget(self.reslotion_dst_le)
 44         self.reslotion_dst_view_btn = QtWidgets.QPushButton(self.tab)
 45         self.reslotion_dst_view_btn.setObjectName("reslotion_dst_view_btn")
 46         self.horizontalLayout_7.addWidget(self.reslotion_dst_view_btn)
 47         self.verticalLayout.addLayout(self.horizontalLayout_7)
 48         self.horizontalLayout = QtWidgets.QHBoxLayout()
 49         self.horizontalLayout.setObjectName("horizontalLayout")
 50         self.label_6 = QtWidgets.QLabel(self.tab)
 51         self.label_6.setObjectName("label_6")
 52         self.horizontalLayout.addWidget(self.label_6)
 53         self.reslotion_srcw_le = QtWidgets.QLineEdit(self.tab)
 54         self.reslotion_srcw_le.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
 55         self.reslotion_srcw_le.setObjectName("reslotion_srcw_le")
 56         self.horizontalLayout.addWidget(self.reslotion_srcw_le)
 57         self.label_7 = QtWidgets.QLabel(self.tab)
 58         self.label_7.setObjectName("label_7")
 59         self.horizontalLayout.addWidget(self.label_7)
 60         self.reslotion_srch_le = QtWidgets.QLineEdit(self.tab)
 61         self.reslotion_srch_le.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
 62         self.reslotion_srch_le.setObjectName("reslotion_srch_le")
 63         self.horizontalLayout.addWidget(self.reslotion_srch_le)
 64         self.label_10 = QtWidgets.QLabel(self.tab)
 65         self.label_10.setObjectName("label_10")
 66         self.horizontalLayout.addWidget(self.label_10)
 67         self.reslotion_dstw_le = QtWidgets.QLineEdit(self.tab)
 68         self.reslotion_dstw_le.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
 69         self.reslotion_dstw_le.setObjectName("reslotion_dstw_le")
 70         self.horizontalLayout.addWidget(self.reslotion_dstw_le)
 71         self.label_9 = QtWidgets.QLabel(self.tab)
 72         self.label_9.setObjectName("label_9")
 73         self.horizontalLayout.addWidget(self.label_9)
 74         self.reslotion_dsth_le = QtWidgets.QLineEdit(self.tab)
 75         self.reslotion_dsth_le.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
 76         self.reslotion_dsth_le.setObjectName("reslotion_dsth_le")
 77         self.horizontalLayout.addWidget(self.reslotion_dsth_le)
 78         self.verticalLayout.addLayout(self.horizontalLayout)
 79         self.horizontalLayout_8 = QtWidgets.QHBoxLayout()
 80         self.horizontalLayout_8.setObjectName("horizontalLayout_8")
 81         spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
 82         self.horizontalLayout_8.addItem(spacerItem)
 83         self.reslotion_submit_btn = QtWidgets.QPushButton(self.tab)
 84         self.reslotion_submit_btn.setEnabled(True)
 85         self.reslotion_submit_btn.setObjectName("reslotion_submit_btn")
 86         self.horizontalLayout_8.addWidget(self.reslotion_submit_btn)
 87         self.verticalLayout.addLayout(self.horizontalLayout_8)
 88         self.tabWidget.addTab(self.tab, "")
 89         self.tab_2 = QtWidgets.QWidget()
 90         self.tab_2.setObjectName("tab_2")
 91         self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.tab_2)
 92         self.verticalLayout_4.setObjectName("verticalLayout_4")
 93         self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
 94         self.horizontalLayout_2.setObjectName("horizontalLayout_2")
 95         self.label = QtWidgets.QLabel(self.tab_2)
 96         self.label.setObjectName("label")
 97         self.horizontalLayout_2.addWidget(self.label)
 98         self.offset_pixel_le = QtWidgets.QLineEdit(self.tab_2)
 99         self.offset_pixel_le.setObjectName("offset_pixel_le")
100         self.horizontalLayout_2.addWidget(self.offset_pixel_le)
101         self.label_2 = QtWidgets.QLabel(self.tab_2)
102         self.label_2.setObjectName("label_2")
103         self.horizontalLayout_2.addWidget(self.label_2)
104         self.offset_meter_le = QtWidgets.QLineEdit(self.tab_2)
105         self.offset_meter_le.setObjectName("offset_meter_le")
106         self.horizontalLayout_2.addWidget(self.offset_meter_le)
107         self.label_3 = QtWidgets.QLabel(self.tab_2)
108         self.label_3.setObjectName("label_3")
109         self.horizontalLayout_2.addWidget(self.label_3)
110         self.verticalLayout_4.addLayout(self.horizontalLayout_2)
111         self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
112         self.horizontalLayout_3.setObjectName("horizontalLayout_3")
113         self.label_4 = QtWidgets.QLabel(self.tab_2)
114         self.label_4.setObjectName("label_4")
115         self.horizontalLayout_3.addWidget(self.label_4)
116         self.offset_dire_le = QtWidgets.QLineEdit(self.tab_2)
117         self.offset_dire_le.setObjectName("offset_dire_le")
118         self.horizontalLayout_3.addWidget(self.offset_dire_le)
119         self.offset_view_btn = QtWidgets.QPushButton(self.tab_2)
120         self.offset_view_btn.setObjectName("offset_view_btn")
121         self.horizontalLayout_3.addWidget(self.offset_view_btn)
122         self.verticalLayout_4.addLayout(self.horizontalLayout_3)
123         self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
124         self.horizontalLayout_4.setObjectName("horizontalLayout_4")
125         self.label_11 = QtWidgets.QLabel(self.tab_2)
126         self.label_11.setObjectName("label_11")
127         self.horizontalLayout_4.addWidget(self.label_11)
128         self.offset_ltx_le = QtWidgets.QLineEdit(self.tab_2)
129         self.offset_ltx_le.setMaximumSize(QtCore.QSize(50, 16777215))
130         self.offset_ltx_le.setObjectName("offset_ltx_le")
131         self.horizontalLayout_4.addWidget(self.offset_ltx_le)
132         self.label_12 = QtWidgets.QLabel(self.tab_2)
133         self.label_12.setObjectName("label_12")
134         self.horizontalLayout_4.addWidget(self.label_12)
135         self.offset_lty_le = QtWidgets.QLineEdit(self.tab_2)
136         self.offset_lty_le.setMaximumSize(QtCore.QSize(50, 16777215))
137         self.offset_lty_le.setObjectName("offset_lty_le")
138         self.horizontalLayout_4.addWidget(self.offset_lty_le)
139         spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
140         self.horizontalLayout_4.addItem(spacerItem1)
141         self.rename_ckb = QtWidgets.QCheckBox(self.tab_2)
142         self.rename_ckb.setObjectName("rename_ckb")
143         self.horizontalLayout_4.addWidget(self.rename_ckb)
144         self.verticalLayout_4.addLayout(self.horizontalLayout_4)
145         self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
146         self.horizontalLayout_6.setObjectName("horizontalLayout_6")
147         spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
148         self.horizontalLayout_6.addItem(spacerItem2)
149         self.offset_submit_btn = QtWidgets.QPushButton(self.tab_2)
150         self.offset_submit_btn.setObjectName("offset_submit_btn")
151         self.horizontalLayout_6.addWidget(self.offset_submit_btn)
152         self.verticalLayout_4.addLayout(self.horizontalLayout_6)
153         self.tabWidget.addTab(self.tab_2, "")
154         self.tab_5 = QtWidgets.QWidget()
155         self.tab_5.setObjectName("tab_5")
156         self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.tab_5)
157         self.verticalLayout_5.setObjectName("verticalLayout_5")
158         self.horizontalLayout_9 = QtWidgets.QHBoxLayout()
159         self.horizontalLayout_9.setObjectName("horizontalLayout_9")
160         self.label_13 = QtWidgets.QLabel(self.tab_5)
161         self.label_13.setObjectName("label_13")
162         self.horizontalLayout_9.addWidget(self.label_13)
163         self.size_directory_le = QtWidgets.QLineEdit(self.tab_5)
164         self.size_directory_le.setObjectName("size_directory_le")
165         self.horizontalLayout_9.addWidget(self.size_directory_le)
166         self.size_view_btn = QtWidgets.QPushButton(self.tab_5)
167         self.size_view_btn.setObjectName("size_view_btn")
168         self.horizontalLayout_9.addWidget(self.size_view_btn)
169         self.verticalLayout_5.addLayout(self.horizontalLayout_9)
170         self.horizontalLayout_10 = QtWidgets.QHBoxLayout()
171         self.horizontalLayout_10.setObjectName("horizontalLayout_10")
172         self.label_14 = QtWidgets.QLabel(self.tab_5)
173         self.label_14.setObjectName("label_14")
174         self.horizontalLayout_10.addWidget(self.label_14)
175         self.size_width_le = QtWidgets.QLineEdit(self.tab_5)
176         self.size_width_le.setInputMethodHints(QtCore.Qt.ImhNone)
177         self.size_width_le.setObjectName("size_width_le")
178         self.horizontalLayout_10.addWidget(self.size_width_le)
179         self.label_15 = QtWidgets.QLabel(self.tab_5)
180         self.label_15.setObjectName("label_15")
181         self.horizontalLayout_10.addWidget(self.label_15)
182         self.size_height_le = QtWidgets.QLineEdit(self.tab_5)
183         self.size_height_le.setInputMethodHints(QtCore.Qt.ImhNone)
184         self.size_height_le.setObjectName("size_height_le")
185         self.horizontalLayout_10.addWidget(self.size_height_le)
186         self.verticalLayout_5.addLayout(self.horizontalLayout_10)
187         spacerItem3 = QtWidgets.QSpacerItem(20, 41, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
188         self.verticalLayout_5.addItem(spacerItem3)
189         self.horizontalLayout_11 = QtWidgets.QHBoxLayout()
190         self.horizontalLayout_11.setObjectName("horizontalLayout_11")
191         spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
192         self.horizontalLayout_11.addItem(spacerItem4)
193         self.size_submit_btn = QtWidgets.QPushButton(self.tab_5)
194         self.size_submit_btn.setObjectName("size_submit_btn")
195         self.horizontalLayout_11.addWidget(self.size_submit_btn)
196         self.verticalLayout_5.addLayout(self.horizontalLayout_11)
197         self.tabWidget.addTab(self.tab_5, "")
198         self.verticalLayout_2.addWidget(self.tabWidget)
199         self.groupBox_3 = QtWidgets.QGroupBox(Dialog)
200         self.groupBox_3.setObjectName("groupBox_3")
201         self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.groupBox_3)
202         self.verticalLayout_3.setObjectName("verticalLayout_3")
203         self.output_lw = QtWidgets.QListWidget(self.groupBox_3)
204         self.output_lw.setObjectName("output_lw")
205         self.verticalLayout_3.addWidget(self.output_lw)
206         self.verticalLayout_2.addWidget(self.groupBox_3)
207         self.verticalLayout_2.setStretch(1, 2)
208 
209         self.retranslateUi(Dialog)
210         self.tabWidget.setCurrentIndex(0)
211         QtCore.QMetaObject.connectSlotsByName(Dialog)
212 
213     def retranslateUi(self, Dialog):
214         _translate = QtCore.QCoreApplication.translate
215         Dialog.setWindowTitle(_translate("Dialog", "IProcesser"))
216         self.label_5.setText(_translate("Dialog", "Src:"))
217         self.reslotion_src_view_btn.setText(_translate("Dialog", "View..."))
218         self.label_8.setText(_translate("Dialog", "Dst:"))
219         self.reslotion_dst_view_btn.setText(_translate("Dialog", "View..."))
220         self.label_6.setText(_translate("Dialog", "SrcW"))
221         self.label_7.setText(_translate("Dialog", "SrcH"))
222         self.label_10.setText(_translate("Dialog", "DstW:"))
223         self.label_9.setText(_translate("Dialog", "DstH:"))
224         self.reslotion_submit_btn.setText(_translate("Dialog", "Submit"))
225         self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("Dialog", "分辨率转换"))
226         self.label.setText(_translate("Dialog", "设定像素比:"))
227         self.offset_pixel_le.setPlaceholderText(_translate("Dialog", "1"))
228         self.label_2.setText(_translate("Dialog", "Pixel"))
229         self.offset_meter_le.setPlaceholderText(_translate("Dialog", "1"))
230         self.label_3.setText(_translate("Dialog", "Meter"))
231         self.label_4.setText(_translate("Dialog", "序列文件夹:"))
232         self.offset_view_btn.setText(_translate("Dialog", "View..."))
233         self.label_11.setText(_translate("Dialog", "LTx:"))
234         self.offset_ltx_le.setPlaceholderText(_translate("Dialog", "0"))
235         self.label_12.setText(_translate("Dialog", "LTy:"))
236         self.offset_lty_le.setPlaceholderText(_translate("Dialog", "0"))
237         self.rename_ckb.setText(_translate("Dialog", "改名"))
238         self.offset_submit_btn.setText(_translate("Dialog", "Submit"))
239         self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("Dialog", "偏移值调整"))
240         self.label_13.setText(_translate("Dialog", "目录:"))
241         self.size_view_btn.setText(_translate("Dialog", "View..."))
242         self.label_14.setText(_translate("Dialog", "设定宽度:"))
243         self.label_15.setText(_translate("Dialog", "设定高度:"))
244         self.size_submit_btn.setText(_translate("Dialog", "Submit"))
245         self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_5), _translate("Dialog", "尺寸调整"))
246         self.groupBox_3.setTitle(_translate("Dialog", "输出"))
247 
248 import IProcessing_rc
View Code
  • Wrap PyUIC生成的代码
  1 # coding=utf-8
  2 import os, shutil, threading, math
  3 from PIL import Image
  4 from PyQt5.QtCore import *
  5 from PyQt5.QtGui import QIcon
  6 from PyQt5.QtWidgets import *
  7 
  8 import IProcessingDialog
  9 import IProcessing_rc
 10 
 11 class IPDW(QDialog, IProcessingDialog.Ui_Dialog):
 12     output_lw_signal = pyqtSignal(str)
 13     reslotion_submit_signal = pyqtSignal()
 14 
 15     def __init__(self):
 16         super(IPDW, self).__init__()
 17         self.setupUi(self)
 18         # icon
 19         self.setWindowIcon(QIcon(':/icon/icon.ico'))
 20         # 信号连接
 21         self.output_lw_signal.connect(self.output_lw_signal_proc)
 22         # 分辨率调整
 23         self.reslotion_src_view_btn.clicked.connect(self.reslotion_src_view_btn_clicked)
 24         self.reslotion_dst_view_btn.clicked.connect(self.reslotion_dst_view_btn_clicked)
 25         self.reslotion_submit_btn.clicked.connect(self.reslotion_submit_btn_clicked)
 26         # 偏移调整
 27         self.offset_view_btn.clicked.connect(self.offset_view_btn_clicked)
 28         self.offset_submit_btn.clicked.connect(self.offset_submit_btn_clicked)
 29         # 尺寸调整
 30         self.size_view_btn.clicked.connect(self.size_view_btn_clicked)
 31         self.size_submit_btn.clicked.connect(self.size_submit_btn_clicked)
 32 
 33     ####################################################################################################################
 34     # 分辨率调整
 35     ####################################################################################################################
 36     # 分辨率调整-源目录-浏览
 37     def reslotion_src_view_btn_clicked(self):
 38         directory = QFileDialog.getExistingDirectory(self, 'Src')
 39         self.reslotion_src_le.setText(directory)
 40 
 41     # 分辨率调整-目标目录-浏览
 42     def reslotion_dst_view_btn_clicked(self):
 43         directory = QFileDialog.getExistingDirectory(self, 'Dst')
 44         self.reslotion_dst_le.setText(directory)
 45 
 46     # 分辨率调整-确定
 47     def reslotion_submit_btn_clicked(self):
 48         self.reslotion_submit_btn.setEnabled(False)
 49         t = threading.Thread(target=self.fact_convert_resolution)
 50         t.start()
 51 
 52     # 分辨率调整-线程工作函数
 53     def fact_convert_resolution(self):
 54         try:
 55             # 删除已经存在转换目标目录
 56             wdire = self.reslotion_dst_le.text()
 57             wdire = os.path.join(wdire, os.path.split(self.reslotion_src_le.text())[-1] + '_convert')
 58             wdire = wdire.replace('\\', '/')
 59             if os.path.exists(wdire): shutil.rmtree(wdire)
 60             wdire = shutil.copytree(self.reslotion_src_le.text(), wdire)
 61             rw, rh = float(self.reslotion_dstw_le.text()) / float(self.reslotion_srcw_le.text()), \
 62                      float(self.reslotion_dsth_le.text()) / float(self.reslotion_srch_le.text())
 63             self.conver_resolution(wdire, rw, rh)
 64         except: QMessageBox.information(self, 'Error', '请检查输入是否完整.')
 65         self.output_lw_signal.emit('全部转换工作已经完成.')
 66         self.reslotion_submit_btn.setEnabled(True)
 67 
 68     ####################################################################################################################
 69     # 偏移值调整
 70     ####################################################################################################################
 71     # 偏移值调整-序列文件夹-浏览
 72     def offset_view_btn_clicked(self):
 73         directory = QFileDialog.getExistingDirectory(self, 'Sequence')
 74         self.offset_dire_le.setText(directory)
 75 
 76     # 偏移值调整-确定
 77     def offset_submit_btn_clicked(self):
 78         dire = self.offset_dire_le.text()
 79         if dire == None:
 80             QMessageBox.information(self, 'Error', '参数错误.')
 81             return
 82         if os.path.isdir(dire) == False:
 83             QMessageBox.information(self, 'Error', '参数错误.')
 84             return
 85         # 遍历文件夹下的图片文件
 86         self.offset_submit_btn.setEnabled(False)
 87         # args是一个数组变量参数,如果只传递一个值,
 88         # 就只需要i, 如果需要传递多个参数,那么还可以继续传递下去其他的参数,
 89         # 其中的逗号不能少,少了就不是数组了,就会出错
 90         t = threading.Thread(target=self.offset_submit_worker, args=(dire,))
 91         t.start()
 92 
 93     # 偏移值调整-线程工作函数
 94     def offset_submit_worker(self, dire):
 95         if not self.offset_pixel_le.text(): pixel = int(self.offset_pixel_le.placeholderText())
 96         else: pixel = int(self.offset_pixel_le.text())
 97         if not self.offset_meter_le.text(): meter = int (self.offset_meter_le.placeholderText())
 98         else: meter = int(self.offset_meter_le.text())
 99         if not self.offset_ltx_le.text(): ltx = float(self.offset_ltx_le.placeholderText())
100         else: ltx = float(self.offset_ltx_le.text())
101         if not self.offset_lty_le.text(): lty = float(self.offset_lty_le.placeholderText())
102         else: lty = float(self.offset_lty_le.text())
103         # 计算偏移像素
104         offx = int(ltx * pixel / meter)
105         offy = int(lty * pixel / meter)
106         # print(offx, offy)
107         # self.output_lw_signal.emit('will process ' + dire)
108         try:
109             n = 0
110             for file in os.listdir(dire):
111                 fn = os.path.join(dire, file)
112                 fn = fn.replace('\\', '/')
113                 # self.output_lw_signal.emit('->' + fn)
114                 if os.path.isfile(fn):
115                     # 分割文件名与文件扩展名
116                     f,e = os.path.splitext(fn)
117                     # 只针对带透明通道的图片
118                     exts = ['.png', '.psd']
119                     if e in exts:
120                         s = Image.open(fn)
121                         sw,sh = s.size
122                         # 调整宽和高为64的倍数
123                         # rw = math.ceil(sw/64.0)
124                         # rh = math.ceil(sh/64.0)
125                         # dw = 64 * rw
126                         dw = sw + abs(int(2 * offx))
127                         if (dw-sw)%2 == 1: dw+=1
128                         # dh = 64 * rh
129                         dh = sh + abs(int(2 * offy))
130                         if (dh-sh)%2 ==1: dh+=1
131                         # print(sw, sh, dw, dh, rw, rh)
132                         # 创建一个容器图片
133                         ns = Image.new('RGBA', (dw,dh))
134                         # 原图无偏移区域
135                         bx = int((dw-sw)/2) + offx
136                         by = int((dh-sh)/2) + offy
137                         # box = (bx,by,sw+bx,sh+by)
138                         # 这里需要源图片尺寸要保持不变
139                         # ns.paste(s,box)
140                         ns.paste(s, (bx,by))
141                         ns.save(fn)
142                         ns.close()
143                         s.close()
144                         # 顺带改名
145                         if self.rename_ckb.isChecked():
146                             nf = os.path.join(os.path.split(fn)[0], '%04d'%(n) + e)
147                             n = n + 1
148                             os.rename(fn, nf)
149                             fn = nf
150                         self.output_lw_signal.emit('->' + fn)
151         except TypeError: print('Error', 'TypeError.')
152         except IOError: print('Error', 'IOError.')
153         except FileNotFoundError: print('Error', 'FileNotFoundError.')
154         self.offset_submit_btn.setEnabled(True)
155         self.output_lw_signal.emit('全部偏移工作已经完成.')
156 
157     # arg-目录 rw-宽度比值 rh-高度比值
158     def conver_resolution(self, arg, rw, rh):
159         if not os.path.isdir(arg) and not os.path.isfile(arg): return False
160         if os.path.isfile(arg):
161             # 分割出目录与文件
162             _strs = os.path.split(arg)
163             # 分割文件名与文件扩展名
164             _strs = _strs[-1].split('.')
165             exts = ['bmp', 'jpeg', 'jpg', 'png', 'psd']
166             if _strs[-1] in exts:
167                 im = Image.open(arg)
168                 w,h = im.size
169                 rim = im.resize((int(w*rw),int(h*rh)), Image.ANTIALIAS)
170                 rim.save(arg)
171                 self.output_lw_signal.emit('->' + arg)
172                 im.close()
173                 rim.close()
174         elif os.path.isdir(arg):
175             for dire in os.listdir(arg):
176                 # 向目录的下一级目录执行操作
177                 narg = os.path.join(arg, dire)
178                 narg = narg.replace('\\', '/')
179                 self.conver_resolution(narg, rw, rh)
180 
181     ####################################################################################################################
182     # 尺寸调整
183     ####################################################################################################################
184     # 尺寸调整-目录-浏览
185     def size_view_btn_clicked(self):
186         directory = QFileDialog.getExistingDirectory(self, '选择需要调整尺寸的目录')
187         self.size_directory_le.setText(directory)
188 
189     # 尺寸调整-确定
190     def size_submit_btn_clicked(self):
191         directory = self.size_directory_le.text()
192         if not directory:
193             self.output_lw_signal.emit('参数输入不完整.')
194             return
195         if not self.size_width_le.text():
196             self.output_lw_signal.emit('参数输入不完整.')
197             return
198         else: self.size_setw = int(self.size_width_le.text())
199         if not self.size_height_le.text():
200             self.output_lw_signal.emit('参数输入不完整.')
201             return
202         else: self.size_seth = int(self.size_height_le.text())
203 
204         self.size_submit_btn.setEnabled(False)
205         t = threading.Thread(target=self.size_submit_btn_callback, args=(directory,))
206         t.start()
207 
208     # 尺寸调整-线程工作函数
209     def size_submit_btn_callback(self, directory):
210         self.output_lw_signal.emit('start process images.')
211         for file in os.listdir(directory):
212             compath = os.path.join(directory, file)
213             compath = compath.replace('\\', '/')
214             if os.path.isfile(compath):
215                 # 分割出文件名与扩展名
216                 f,e = os.path.splitext(compath)
217                 # 确定操作的文件为图片
218                 if e in ['.png', '.psd', '.jpg', '.jpeg','.bmp']:
219                     im = Image.open(compath)
220                     imw,imh = im.size
221                     x = abs(int((imw - self.size_setw) / 2))
222                     y = abs(int((imh - self.size_seth) / 2))
223                     if im.size >= (self.size_setw, self.size_seth):
224                         newim = im.crop((x, y, x + self.size_setw, y + self.size_seth))
225                     else:
226                         newim = Image.new('RGBA', (self.size_setw, self.size_seth))
227                         newim.paste(im, (x, y))
228                     newim.save(compath)
229                     newim.close()
230                     im.close()
231                     self.output_lw_signal.emit(compath + ' process success.')
232         self.size_submit_btn.setEnabled(True)
233         self.output_lw_signal.emit('all process is completed.')
234 
235     ####################################################################################################################
236     # comm
237     ####################################################################################################################
238     def output_lw_signal_proc(self, arg):
239         self.output_lw.addItem(arg)
240         self.output_lw.scrollToBottom()
241 
242     def reslotion_submit_signal_proc(self):
243         self.reslotion_submit_btn.setEnabled(True)
View Code
  • 程序入口
 1 # coding=utf-8
 2 import sys
 3 from PyQt5.QtCore import *
 4 from PyQt5.QtWidgets import *
 5 from IProcessingDialogWrap import IPDW
 6 
 7 if __name__ == '__main__':
 8     app = QApplication(sys.argv)
 9     ip = IPDW()
10     ip.show()
11     sys.exit(app.exec_())

 

posted @ 2017-09-10 13:29  非法关键字  阅读(1055)  评论(0编辑  收藏  举报