-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAutoClip.py
More file actions
279 lines (233 loc) · 8.52 KB
/
AutoClip.py
File metadata and controls
279 lines (233 loc) · 8.52 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QFileDialog, QTreeWidgetItem
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.Qt import Qt
import sys
import os
from windows import ReMainWindow
import platform
import subprocess
from clip_image import request_ai, cut
import time
class ClipThread(QThread):
output = pyqtSignal(bool, str, str)
over = pyqtSignal()
def __init__(self, parent=None):
self.is_running = False
super(ClipThread, self).__init__(parent)
def run(self) -> None:
self.is_running = self.parent().is_running
image_file_path = self.parent().image_file_path
for filename in os.listdir(image_file_path):
if os.path.splitext(filename)[1] in [".jpg", ".png", ".bmp", ".JPG", ".PNG", ".BMP"]:
filepath = os.path.join(image_file_path, filename)
if self.is_running:
try:
self.output.emit(0, filename, "")
status, rect = request_ai(filepath)
if status:
is_true, new_path = cut(
filepath, self.parent().save_path, rect, self.parent().bg_color, self.parent().clip_width, self.parent().clip_height
)
self.output.emit(1, filepath, new_path)
else:
self.output.emit(2, filepath, rect)
except Exception as e:
self.output.emit(2, filepath, f"{e}")
continue
else:
self.over.emit()
return
self.sleep(1)
self.over.emit()
def stop(self):
self.is_running = False
class MainWindow(QMainWindow, ReMainWindow):
def __init__(self, parent=None, flags=Qt.WindowFlags()):
super(MainWindow, self).__init__(parent, flags)
self.setupUi(self)
self.home_path = os.path.join(os.environ["USERPROFILE"], "Desktop")
self.image_file_path = None
self.get_save_path_history()
self.bg_color = (0, 0, 0)
self.save_path = ""
self.clip_width = None
self.clip_height = None
self.image_counts = 0
self.success_counts = 0
self.is_running = False
self.userPlatform = platform.system()
self.pushButton.clicked.connect(self.load_image_files)
self.treeWidget.itemDoubleClicked.connect(self.show_image)
self.toolButton.clicked.connect(self.get_save_path)
self.pushButton_2.clicked.connect(self.start_clip)
self.clip_thread = ClipThread(self)
self.clip_thread.output.connect(self.output_message)
self.clip_thread.over.connect(self.over_clip)
self.comboBox_3.activated.connect(self.update_save_item)
def update_save_item(self):
current_index = self.comboBox_3.currentIndex()
with open("save_path.txt", "r+", encoding="utf8") as f:
lines = f.readlines()
text = lines[current_index]
lines.append(text)
lines.pop(current_index)
with open("save_path.txt", "w+", encoding="utf8") as ff:
for line in reversed(lines):
ff.write(line)
def format_message(self, message):
t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
return f"[{t}] {message}"
def output_message(self, status, source, output):
if status == 0:
self.image_counts += 1
color = "blue"
message = self.format_message(f"<font style='color: {color};'>开始裁剪: {os.path.split(source)[1]}</font>")
elif status == 1:
color = "#55aa7f"
message = self.format_message(f"<font style='color: {color};'>裁剪成功</font>")
self.success_counts += 1
elif status == 2:
color = "red"
message = self.format_message(f"<font style='color: {color};'>裁剪失败-{output}</font>")
self.textEdit.append(message)
def over_clip(self):
self.pushButton_2.setText("开始裁剪")
box = QMessageBox(
QMessageBox.Information, "裁剪完成",
f"共裁剪图片 {self.image_counts} 张\n成功裁剪 {self.success_counts} 张\n裁剪失败 {self.image_counts - self.success_counts}"
)
yes = box.addButton(self.tr("打开文件夹"), QMessageBox.YesRole)
box.addButton(self.tr("确定"), QMessageBox.NoRole)
box.exec_()
if box.clickedButton() == yes:
os.startfile(self.save_path)
self.image_counts = 0
self.success_counts = 0
self.enabled()
def get_bgcolor(self):
index = self.comboBox_2.currentIndex()
if index:
return 255, 255, 255
else:
return 0, 0, 0
def start_clip(self):
if not self.image_file_path:
QMessageBox.warning(self, "参数错误", "请导入需要裁剪的图片", QMessageBox.Ok)
return
if self.comboBox.currentIndex() == 1:
if not self.lineEdit.text() or not self.lineEdit_2.text():
QMessageBox.warning(self, "参数错误", "自定义裁剪需要设定图像尺寸", QMessageBox.Ok)
return
self.clip_width = int(self.lineEdit.text())
self.clip_height = int(self.lineEdit_2.text())
else:
self.clip_width = None
self.clip_height = None
if not self.comboBox_3.currentText():
QMessageBox.warning(self, "参数错误", "请设置图片保存路径", QMessageBox.Ok)
return
self.bg_color = self.get_bgcolor()
self.save_path = self.comboBox_3.currentText()
if not self.is_running:
self.pushButton_2.setText("停止裁剪")
self.is_running = not self.is_running
self.unenable()
self.clip_thread.start()
self.textEdit.clear()
else:
self.enabled()
self.is_running = not self.is_running
self.clip_thread.stop()
def enabled(self):
self.comboBox.setEnabled(True)
if self.comboBox.currentIndex():
self.lineEdit.setEnabled(True)
self.lineEdit_2.setEnabled(True)
self.comboBox_2.setEnabled(True)
self.comboBox_3.setEnabled(True)
self.pushButton.setEnabled(True)
self.toolButton.setEnabled(True)
self.is_running = not self.is_running
def unenable(self):
self.lineEdit.setEnabled(False)
self.lineEdit_2.setEnabled(False)
self.comboBox.setEnabled(False)
self.comboBox_2.setEnabled(False)
self.comboBox_3.setEnabled(False)
self.pushButton.setEnabled(False)
self.toolButton.setEnabled(False)
def get_save_path_history(self):
if os.path.exists("save_path.txt"):
with open("save_path.txt", "r+", encoding="utf8") as f:
lines = f.readlines()
if len(lines) > 5:
with open("save_path.txt", "w+", encoding="utf8") as ff:
for line in lines[-5:]:
ff.write(line)
else:
with open("save_path.txt", "w+", encoding="utf8"):
pass
with open("save_path.txt", "r+", encoding="utf8") as fff:
for index, line in enumerate(fff.readlines()):
self.comboBox_3.insertItem(index, line.strip())
def get_save_path(self):
save_path = QFileDialog.getExistingDirectory(self, "选择图片保存路径", self.home_path)
if save_path:
self.comboBox_3.insertItem(0, save_path)
self.comboBox_3.setCurrentIndex(0)
with open("save_path.txt", "a+", encoding="utf8") as f:
f.write(save_path+"\n")
def show_image(self, item, column):
text = item.text(0)
file_name = os.path.join(self.image_file_path, text)
if os.path.isfile(file_name):
if self.userPlatform == 'Darwin': # Mac
subprocess.call(['open', file_name])
elif self.userPlatform == 'Linux': # Linux
subprocess.call(['xdg-open', file_name])
else: # Windows
os.startfile(file_name)
def load_image_files(self):
image_file_path = QFileDialog.getExistingDirectory(self, "选择导入的图片文件夹", self.home_path)
if image_file_path:
self.image_file_path = image_file_path
self.set_file_tree()
def set_file_tree(self):
self.treeWidget.clear()
root_name = os.path.basename(self.image_file_path)
root = QTreeWidgetItem(self.treeWidget)
root.setText(0, root_name)
root.setIcon(0, QIcon("images/folder.png"))
for filename in os.listdir(self.image_file_path):
if os.path.splitext(filename)[1] in [".jpg", ".png", ".bmp", ".JPG", ".PNG", ".BMP"]:
# self.image_counts += 1
child = QTreeWidgetItem(root)
child.setText(0, filename)
child.setIcon(0, QIcon("images/picture.png"))
self.treeWidget.expandAll()
# for dirpath, dirnames, filenames in os.walk(self.image_file_path):
# level = dirpath.replace(self.image_file_path, "").count(os.sep)
# if level:
# child_root = QTreeWidgetItem(root)
# child_root.setText(0, os.path.basename(dirpath))
# child_root.setIcon(0, QIcon("images/folder.png"))
# for filename in filenames:
# child = QTreeWidgetItem(child_root)
# child.setText(0, filename)
# child.setIcon(0, QIcon("images/picture.png"))
# # for dirname in dirnames:
# # child_root = QTreeWidgetItem(root)
# # child_root.setText(0, dirname)
# # child_root.setIcon(0, QIcon("images/folder.png"))
# else:
# for filename in filenames:
# child = QTreeWidgetItem(root)
# child.setText(0, filename)
# child.setIcon(0, QIcon("images/picture.png"))
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())