码迷,mamicode.com
首页 > 其他好文 > 详细

写个简单的图片素材处理工具

时间:2017-09-10 14:24:54      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:inpu   一个   upx   mini   worker   inf   sed   tao   遍历文件   

  • 通过PyUIC生成的代码
技术分享
  1 # -*- coding: utf-8 -*-
  2 
  3 # Form implementation generated from reading ui file ‘E:\Development\IProcessing/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.verticalLayout_2.addWidget(self.tabWidget)
155         self.groupBox_3 = QtWidgets.QGroupBox(Dialog)
156         self.groupBox_3.setObjectName("groupBox_3")
157         self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.groupBox_3)
158         self.verticalLayout_3.setObjectName("verticalLayout_3")
159         self.output_lw = QtWidgets.QListWidget(self.groupBox_3)
160         self.output_lw.setObjectName("output_lw")
161         self.verticalLayout_3.addWidget(self.output_lw)
162         self.verticalLayout_2.addWidget(self.groupBox_3)
163         self.verticalLayout_2.setStretch(1, 2)
164 
165         self.retranslateUi(Dialog)
166         self.tabWidget.setCurrentIndex(0)
167         QtCore.QMetaObject.connectSlotsByName(Dialog)
168 
169     def retranslateUi(self, Dialog):
170         _translate = QtCore.QCoreApplication.translate
171         Dialog.setWindowTitle(_translate("Dialog", "IProcessing"))
172         self.label_5.setText(_translate("Dialog", "Src:"))
173         self.reslotion_src_view_btn.setText(_translate("Dialog", "View..."))
174         self.label_8.setText(_translate("Dialog", "Dst:"))
175         self.reslotion_dst_view_btn.setText(_translate("Dialog", "View..."))
176         self.label_6.setText(_translate("Dialog", "SrcW"))
177         self.label_7.setText(_translate("Dialog", "SrcH"))
178         self.label_10.setText(_translate("Dialog", "DstW:"))
179         self.label_9.setText(_translate("Dialog", "DstH:"))
180         self.reslotion_submit_btn.setText(_translate("Dialog", "Submit"))
181         self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("Dialog", "转换分辨率"))
182         self.label.setText(_translate("Dialog", "设定像素比:"))
183         self.offset_pixel_le.setPlaceholderText(_translate("Dialog", "1"))
184         self.label_2.setText(_translate("Dialog", "Pixel"))
185         self.offset_meter_le.setPlaceholderText(_translate("Dialog", "1"))
186         self.label_3.setText(_translate("Dialog", "Meter"))
187         self.label_4.setText(_translate("Dialog", "序列文件夹:"))
188         self.offset_view_btn.setText(_translate("Dialog", "View..."))
189         self.label_11.setText(_translate("Dialog", "LTx:"))
190         self.offset_ltx_le.setPlaceholderText(_translate("Dialog", "0"))
191         self.label_12.setText(_translate("Dialog", "LTy:"))
192         self.offset_lty_le.setPlaceholderText(_translate("Dialog", "0"))
193         self.rename_ckb.setText(_translate("Dialog", "改名"))
194         self.offset_submit_btn.setText(_translate("Dialog", "Submit"))
195         self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("Dialog", "偏移值调整"))
196         self.groupBox_3.setTitle(_translate("Dialog", "输出"))
197 
198 import IProcessing_rc
View Code
  • 包装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         # tab1
 21         self.output_lw_signal.connect(self.output_lw_signal_proc)
 22         self.reslotion_src_view_btn.clicked.connect(self.reslotion_src_view_btn_clicked)
 23         self.reslotion_dst_view_btn.clicked.connect(self.reslotion_dst_view_btn_clicked)
 24         self.reslotion_submit_btn.clicked.connect(self.reslotion_submit_btn_clicked)
 25         # tab2
 26         self.offset_view_btn.clicked.connect(self.offset_view_btn_clicked)
 27         self.offset_submit_btn.clicked.connect(self.offset_submit_btn_clicked)
 28 
 29     # 按钮事件
 30     def reslotion_src_view_btn_clicked(self):
 31         directory = QFileDialog.getExistingDirectory(self, Src)
 32         self.reslotion_src_le.setText(directory)
 33 
 34     def reslotion_dst_view_btn_clicked(self):
 35         directory = QFileDialog.getExistingDirectory(self, Dst)
 36         self.reslotion_dst_le.setText(directory)
 37 
 38     def reslotion_submit_btn_clicked(self):
 39         self.reslotion_submit_btn.setEnabled(False)
 40         t = threading.Thread(target=self.fact_convert_resolution)
 41         t.start()
 42 
 43     def fact_convert_resolution(self):
 44         try:
 45             # 删除已经存在转换目标目录
 46             wdire = self.reslotion_dst_le.text()
 47             wdire = os.path.join(wdire, os.path.split(self.reslotion_src_le.text())[-1] + _convert)
 48             wdire = wdire.replace(\\, /)
 49             if os.path.exists(wdire): shutil.rmtree(wdire)
 50             wdire = shutil.copytree(self.reslotion_src_le.text(), wdire)
 51             rw, rh = float(self.reslotion_dstw_le.text()) / float(self.reslotion_srcw_le.text()),  52                      float(self.reslotion_dsth_le.text()) / float(self.reslotion_srch_le.text())
 53             self.conver_resolution(wdire, rw, rh)
 54         except: QMessageBox.information(self, Error, 请检查输入是否完整.)
 55         self.output_lw_signal.emit(全部转换工作已经完成.)
 56         self.reslotion_submit_btn.setEnabled(True)
 57 
 58     def offset_view_btn_clicked(self):
 59         directory = QFileDialog.getExistingDirectory(self, Sequence)
 60         self.offset_dire_le.setText(directory)
 61 
 62     def offset_submit_btn_clicked(self):
 63         dire = self.offset_dire_le.text()
 64         if dire == None:
 65             QMessageBox.information(self, Error, 参数错误.)
 66             return
 67         if os.path.isdir(dire) == False:
 68             QMessageBox.information(self, Error, 参数错误.)
 69             return
 70         # 遍历文件夹下的图片文件
 71         self.offset_submit_btn.setEnabled(False)
 72         # args是一个数组变量参数,如果只传递一个值,
 73         # 就只需要i, 如果需要传递多个参数,那么还可以继续传递下去其他的参数,
 74         # 其中的逗号不能少,少了就不是数组了,就会出错
 75         t = threading.Thread(target=self.offset_submit_worker, args=(dire,))
 76         t.start()
 77 
 78     # 工作函数
 79     def offset_submit_worker(self, dire):
 80         if not self.offset_pixel_le.text(): pixel = int(self.offset_pixel_le.placeholderText())
 81         else: pixel = int(self.offset_pixel_le.text())
 82         if not self.offset_meter_le.text(): meter = int (self.offset_meter_le.placeholderText())
 83         else: meter = int(self.offset_meter_le.text())
 84         if not self.offset_ltx_le.text(): ltx = float(self.offset_ltx_le.placeholderText())
 85         else: ltx = float(self.offset_ltx_le.text())
 86         if not self.offset_lty_le.text(): lty = float(self.offset_lty_le.placeholderText())
 87         else: lty = float(self.offset_lty_le.text())
 88         # 计算偏移像素
 89         offx = int(ltx * pixel / meter)
 90         offy = int(lty * pixel / meter)
 91         # print(offx, offy)
 92         # self.output_lw_signal.emit(‘will process ‘ + dire)
 93         try:
 94             n = 0
 95             for file in os.listdir(dire):
 96                 fn = os.path.join(dire, file)
 97                 fn = fn.replace(\\, /)
 98                 # self.output_lw_signal.emit(‘->‘ + fn)
 99                 if os.path.isfile(fn):
100                     # 分割文件名与文件扩展名
101                     f,e = os.path.splitext(fn)
102                     # 只针对带透明通道的图片
103                     exts = [.png, .psd]
104                     if e in exts:
105                         s = Image.open(fn)
106                         sw,sh = s.size
107                         # 调整宽和高为64的倍数
108                         # rw = math.ceil(sw/64.0)
109                         # rh = math.ceil(sh/64.0)
110                         # dw = 64 * rw
111                         dw = sw + abs(int(2 * offx))
112                         if (dw-sw)%2 == 1: dw+=1
113                         # dh = 64 * rh
114                         dh = sh + abs(int(2 * offy))
115                         if (dh-sh)%2 ==1: dh+=1
116                         # print(sw, sh, dw, dh, rw, rh)
117                         # 创建一个容器图片
118                         ns = Image.new(RGBA, (dw,dh))
119                         # 原图无偏移区域
120                         bx = int((dw-sw)/2) + offx
121                         by = int((dh-sh)/2) + offy
122                         # box = (bx,by,sw+bx,sh+by)
123                         # 这里需要源图片尺寸要保持不变
124                         # ns.paste(s,box)
125                         ns.paste(s, (bx,by))
126                         ns.save(fn)
127                         ns.close()
128                         s.close()
129                         # 顺带改名
130                         if self.rename_ckb.isChecked():
131                             nf = os.path.join(os.path.split(fn)[0], %04d%(n) + e)
132                             n = n + 1
133                             os.rename(fn, nf)
134                             fn = nf
135                         self.output_lw_signal.emit(-> + fn)
136         except TypeError: print(Error, TypeError.)
137         except IOError: print(Error, IOError.)
138         except FileNotFoundError: print(Error, FileNotFoundError.)
139         self.offset_submit_btn.setEnabled(True)
140         self.output_lw_signal.emit(全部偏移工作已经完成.)
141 
142     # arg-目录 rw-宽度比值 rh-高度比值
143     def conver_resolution(self, arg, rw, rh):
144         if not os.path.isdir(arg) and not os.path.isfile(arg): return False
145         if os.path.isfile(arg):
146             # 分割出目录与文件
147             _strs = os.path.split(arg)
148             # 分割文件名与文件扩展名
149             _strs = _strs[-1].split(.)
150             exts = [bmp, jpeg, jpg, png, psd]
151             if _strs[-1] in exts:
152                 im = Image.open(arg)
153                 w,h = im.size
154                 rim = im.resize((int(w*rw),int(h*rh)), Image.ANTIALIAS)
155                 rim.save(arg)
156                 self.output_lw_signal.emit(-> + arg)
157                 im.close()
158                 rim.close()
159         elif os.path.isdir(arg):
160             for dire in os.listdir(arg):
161                 # 向目录的下一级目录执行操作
162                 narg = os.path.join(arg, dire)
163                 narg = narg.replace(\\, /)
164                 self.conver_resolution(narg, rw, rh)
165 
166     def output_lw_signal_proc(self, arg):
167         self.output_lw.addItem(arg)
168         self.output_lw.scrollToBottom()
169 
170     def reslotion_submit_signal_proc(self):
171         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_())
View Code

技术分享技术分享

pyinstaller -w --noupx -n IProcessing-Builder20170626 -i icon.ico  IProcessingMain.py

技术分享

 

写个简单的图片素材处理工具

标签:inpu   一个   upx   mini   worker   inf   sed   tao   遍历文件   

原文地址:http://www.cnblogs.com/linxmouse/p/7500887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!