Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
245 lines (200 loc) · 8.12 KB
/
Copy pathmain.py
File metadata and controls
245 lines (200 loc) · 8.12 KB
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
from unittest import result
import cv2
import os
import numpy as np
import os
import stat
import time
from pathlib import Path
from PyQt6 import QtWidgets, QtCore
from PyQt6.QtGui import QGuiApplication
from tools.tiff_processer import TIFF
from ui.Ui_MainWindow import Ui_MainWindow
from ui.inputDialog import Ui_Dialog
from tools.raw_processer import Raw
from tools.png_processer import PNG
from tools.jpg_processer import JPG
# 支持的格式, 不在其中的会跳过处理
FILE_TYPES = ('.jpg', '.jpge', '.png', '.raw', '.tif', '.tiff')
class ToolWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(ToolWindow, self).__init__(parent)
def openFile(self):
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "打开文件", os.getcwd(
), "All Images(*.jpg;*jpge;*.png;*.raw;*.tiff;*.tif);;PNG(*.png);;Photoshop RAW(*.raw);;JEPE(*.jpg;*jpge);;TIFF(*.tif;*.tiff)")
self.lineEdit_input.setText(fileName)
def ouputDir(self):
dirPath = QtWidgets.QFileDialog.getExistingDirectory(
self, "选择输出路径", os.getcwd())
self.lineEdit_output.setText(dirPath)
def showInputDialog(self):
if self.inputDialog.exec() == 1:
return True
else:
return False
def checkRawInput(self):
# width = int(self.inputDialogUI.lineEdit_width.text())
# height = int(self.inputDialogUI.lineEdit_height.text())
# channels = int(self.inputDialogUI.lineEdit_channels.text())
try:
width = int(self.inputDialogUI.lineEdit_width.text())
except ValueError as e:
QtWidgets.QMessageBox.warning(
self, "提示", "Raw宽度不是一个整数!")
return False
try:
width = int(self.inputDialogUI.lineEdit_width.text())
except ValueError as e:
QtWidgets.QMessageBox.warning(
self, "提示", "Raw宽度不是一个整数!")
return False
try:
width = int(self.inputDialogUI.lineEdit_width.text())
except ValueError as e:
QtWidgets.QMessageBox.warning(
self, "提示", "Raw通道不是一个整数!")
return False
return True
def center(self):
screen = QGuiApplication.screenAt(QtCore.QPoint(
self.frameGeometry().x(), self.frameGeometry().y()))
qr = self.frameGeometry()
cp = screen.availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def initUI(self, MainWindow):
self.setupUi(MainWindow)
self.pushButton_start = self.pushButton_3
self.center()
self.inputDialog = QtWidgets.QDialog(self)
self.inputDialogUI = Ui_Dialog()
self.inputDialogUI.setupUi(self.inputDialog)
# 打开文件
self.pushButton_input.clicked.connect(self.openFile)
# self.pushButton_input.clicked.connect(self.test)
# 输出目录
self.pushButton_output.clicked.connect(self.ouputDir)
# 开始处理
self.pushButton_start.clicked.connect(self.start)
def setIsEnabled(self, isEnabled):
self.lineEdit_input.setEnabled(isEnabled)
self.lineEdit_output.setEnabled(isEnabled)
self.pushButton_input.setEnabled(isEnabled)
self.pushButton_output.setEnabled(isEnabled)
self.lineEdit_row.setEnabled(isEnabled)
self.lineEdit_col.setEnabled(isEnabled)
self.pushButton_start.setEnabled(isEnabled)
# 等分
def start(self):
if(self.checkInput() == False):
return
inputPath = self.lineEdit_input.text()
_, extension_name = os.path.splitext(inputPath)
extension_name = extension_name.lower()
if extension_name == '.raw':
result = self.showInputDialog()
if not result:
return
if self.checkRawInput() == False:
return
self.handle()
def handle(self):
# 禁用所有控件
self.setIsEnabled(False)
# 隐藏开始按钮
self.pushButton_start.setVisible(False)
# 显示进度条
self.progressBar.setVisible(True)
self.progressBar.setValue(0)
self.handle()
def complete(self):
self.pushButton_start.setVisible(True)
self.progressBar.setVisible(False)
self.setIsEnabled(True)
def test(self):
QtWidgets.QMessageBox.warning(
self, "提示", "输入文件不存在或类型错误!")
def checkInput(self):
inputPath = Path(self.lineEdit_input.text())
outputPath = Path(self.lineEdit_output.text())
if not inputPath.is_file() or inputPath.suffix not in FILE_TYPES:
QtWidgets.QMessageBox.warning(
self, "提示", "输入文件不存在或类型错误!")
return False
if not outputPath.is_dir():
QtWidgets.QMessageBox.warning(
self, "提示", "输出目录不存在!")
return False
# print(os.access(outputPath, os.X_OK | os.R_OK | os.X_OK | os.F_OK))
# print(os.access(outputPath, os.R_OK))
if not os.access(self.lineEdit_output.text(), os.W_OK):
print(os.access(outputPath, os.X_OK))
QtWidgets.QMessageBox.warning(
self, "提示", "输出目录没有写入权限,请重新选择!")
return False
# os.chmod(outputPath, stat.S_IWRITE)
try:
f = open(self.lineEdit_output.text() + '/test.txt', 'w')
f.close()
os.remove(self.lineEdit_output.text() + '/test.txt')
except PermissionError:
QtWidgets.QMessageBox.warning(
self, "提示", "输出目录没有写入权限,请重新选择!")
return False
try:
row = int(self.lineEdit_row.text())
except ValueError as e:
QtWidgets.QMessageBox.warning(
self, "提示", "行数不是一个整数!")
return False
try:
col = int(self.lineEdit_col.text())
except ValueError as e:
QtWidgets.QMessageBox.warning(
self, "提示", "列数不是一个整数!")
return False
return True
def showProgress(self, value):
self.progressBar.setValue(value)
def handle(self):
inputPath = self.lineEdit_input.text()
outputPath = self.lineEdit_output.text()
row = int(self.lineEdit_row.text())
col = int(self.lineEdit_col.text())
_, extension_name = os.path.splitext(inputPath)
extension_name = extension_name.lower()
if extension_name not in FILE_TYPES:
raise Exception("文件打开失败")
if extension_name == '.raw':
width = int(self.inputDialogUI.lineEdit_width.text())
height = int(self.inputDialogUI.lineEdit_height.text())
channels = int(self.inputDialogUI.lineEdit_channels.text())
processer = Raw(inputPath, outputPath, row, col,(height, width, channels))
if extension_name == '.png':
processer = PNG(inputPath, outputPath, row, col)
if extension_name == '.jpg' or extension_name == '.jpge':
processer = JPG(inputPath, outputPath, row, col)
if extension_name == '.tif' or extension_name == '.tiff':
processer = TIFF(inputPath, outputPath, row, col)
if not processer:
self.complete()
return
processer.update.connect(self.showProgress)
time.sleep(0.5)
result = processer.slice()
if isinstance(result, str):
QtWidgets.QMessageBox.information(
self, "提示", result)
else:
time.sleep(0.5)
QtWidgets.QMessageBox.information(
self, "处理完成提示", "恭喜,处理完成!")
self.complete()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = ToolWindow()
ui.initUI(MainWindow)
MainWindow.show()
sys.exit(app.exec())