-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
293 lines (218 loc) · 7.46 KB
/
run.py
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# @author: CypherpunkSamurai
# @license: MIT
# A program to assist in tagging of images
import sys
import shelve # for settings
from PyQt5 import QtWidgets, uic
# Required
from PyQt5.QtWidgets import qApp
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtWidgets import QMessageBox
# Image
from PyQt5.QtWidgets import QGraphicsScene
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt, QRectF
# Lists
from src import utils
import os
# theme
from PyQt5.QtWidgets import QStyle
from PyQt5.QtWidgets import qApp
# Use MainWindow UI
from src.ui.MainWindow import Ui_MainWindow
from src.ui.Theme import ThemeChooseDlg
__version__ = 0.1
__author__ = "Cypherpunk Samurai"
__author_email__ = "cypherpunksamurai@protonmail.com"
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
"""
Main Window Class
"""
current_folder = None
current_image = None
# image handling
current_image_pixmap = None
current_scene = None
def __init__(self, *args, obj=None, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
self.bind_connect()
def open_folder(self):
"""
Open a project folder
"""
# Open Folder
current_folder = QFileDialog.getExistingDirectory(self, "Select Directory")
# If not selected
print(current_folder)
if current_folder == None or current_folder == "":
print("No folder was selected...")
return
else:
self.current_folder = str(current_folder)
# List Files
self.listFile.addItems(utils.list_images(self.current_folder))
def close_folder(self):
"""
Handles File Menu "Close"
"""
# Close the folder
self.current_folder = None
self.listFile.clear()
self.img.setScene(QGraphicsScene())
self.txtCaption.clear()
# btn
self.btnSaveCaption.setEnabled(False)
# Status
self.setStatusTip("Ready")
def message(self, text):
"""
Message
"""
QMessageBox.information(self, "Info", text)
def list_item_select(self, item):
"""
Runs when the list item is clicked
"""
# get file
text = item.text()
file_path = os.path.join(self.current_folder, text)
# path
if not os.path.exists(file_path):
print("file not found")
return
self.current_image = file_path
# load the image
self.load_image()
# render the new image
self.render_scene()
def load_image(self, url=None):
"""
loads a image to a self.img_scene pixmap for rendering and renders
"""
if url == None and not self.current_image == None: url=self.current_image
else: return
print("selected image: " + url)
self.img.setAlignment(Qt.AlignCenter)
# set
self.current_image_pixmap = QPixmap(url)
self.img_scene = QGraphicsScene()
self.img_scene.addPixmap(self.current_image_pixmap)
# btn
self.btnSaveCaption.setEnabled(True)
# load caption if exists
self.load_caption()
def render_scene(self):
"""
Render self.img_scene without reloading the image
"""
# Scene
self.img.setScene(self.img_scene)
# Scale
self.img.fitInView(QRectF(0, 0, self.current_image_pixmap.width(), self.current_image_pixmap.height()), Qt.KeepAspectRatio)
def resizeEvent(self, event):
"""
Overrides the resize event
"""
# render image everytime window resizes
if self.current_image: self.render_scene()
# resize
QtWidgets.QMainWindow.resizeEvent(self, event)
def load_caption(self, filename=None):
"""
Load caption from txt if exits
"""
# filename
if filename == None:
filename = os.path.join(self.current_folder, self.listFile.currentItem().text())
# caption file
caption_path = utils.change_file_ext(filename, "txt")
# caption
caption = None
if os.path.exists(caption_path):
caption = self.read_caption(caption_path)
self.txtCaption.document().setPlainText(caption)
def btn_save_caption_clicked(self):
"""
Handles SaveCaption button click
"""
# cap
caption = self.txtCaption.document().toPlainText()
# filename
filename = os.path.join(self.current_folder, self.listFile.currentItem().text())
# caption file
caption_path = utils.change_file_ext(filename, "txt")
self.save_caption(caption_path, caption)
def save_caption(self, filename, caption):
"""
Save the caption in a txt file
"""
with open(filename, "w+") as file:
file.write(caption)
print(f"[+] {filename}")
print("Saved caption...")
def read_caption(self, filename):
"""
Read the caption from a txt file
"""
caption = None
with open(filename, "r", encoding="utf-8", errors="ignore") as file:
caption = file.read()
return caption
def choose_theme_dialog(self):
"""
Show a choose theme dialog
"""
dlg = ThemeChooseDlg()
if dlg.exec_():
values = dlg.getResult()
QtWidgets.QApplication.setStyle(values)
# save current settings
try:
with shelve.open("config") as settings:
settings["current_style"] = values
except Exception as e:
print("cannot write current_style to shelve. ", e)
return
else:
print("no theme chosen")
return
def about(self):
self.message(f"Captioner {__version__}\n\nA Captioning tool created for image captioning. \n\n💻Authour: {__author__}\n📫Email: {__author_email__}")
def bind_connect(self):
"""
Connection Binder
"""
# Exit
self.mnuExit.triggered.connect(sys.exit)
# Folder Select
self.mnuOpenFolder.triggered.connect(self.open_folder)
# Folder Close
self.mnuCloseFolder.triggered.connect(self.close_folder)
# Theme
self.mnuTheme.triggered.connect(self.choose_theme_dialog)
# About
self.mnuAbout.triggered.connect(self.about)
# ListWidget
self.listFile.itemClicked.connect(self.list_item_select)
# Caption Button
self.btnSaveCaption.clicked.connect(self.btn_save_caption_clicked)
if __name__ == "__main__":
# check and read style
current_style = None
# read settings
if os.path.exists("config.dat"):
try:
with shelve.open("config") as settings:
if "current_style" in settings: current_style = settings["current_style"]
except Exception as e:
print(e)
finally: pass
# Run
app = QtWidgets.QApplication(sys.argv)
# Style
if current_style: app.setStyle(current_style)
# Windows
window = MainWindow()
window.show()
app.exec()