-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
94 lines (68 loc) · 1.94 KB
/
common.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
import json
import os
import shutil
calibrate_win_w = 800
calibrate_win_h = 600
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import filedialog
from tkinter import messagebox
config_file = "BoxToolBox.json"
def load_config(path):
cfg_file = os.path.join(path, config_file)
if os.path.exists(cfg_file):
f = open(cfg_file, "r")
try:
cfg = json.load(f)
except:
cfg = {}
f.close()
return cfg
return {}
def store_config(cfg, path):
cfg_file = os.path.join(path, config_file)
f = open(cfg_file, "w")
cfg = json.dump(cfg, f, sort_keys=True, indent=4, separators=(',', ': '))
f.close()
def read_dir(path):
lst = os.listdir(path)
lst.sort()
images = []
for file in lst:
ext = os.path.basename(file).split(".")
if len(ext) == 2:
if ext[1].upper() == "JPG" or ext[1].upper() == "PNG":
images.append(file)
return images
def get_dir():
root = tk.Tk()
root.withdraw()
return filedialog.askdirectory(title="Select folder with images", mustexist = True)
def create_progressbar():
ws = tk.Tk()
ws.title('BoxToolBox')
ws.geometry('200x80')
ws.resizable(False, False)
ws.eval('tk::PlaceWindow . center')
ws.deiconify()
title = ttk.Label(ws)
title.pack(expand=True)
text = ttk.Label(ws)
text.pack(expand=True)
bar = ttk.Progressbar(ws, orient=tk.HORIZONTAL, length=150, mode='determinate')
bar.pack(expand=True)
return [ws, title, text, bar]
def update_progressbar(win, title, text, progress):
win[1]["text"] = title
win[2]["text"] = text
win[3]["value"] = progress
win[0].update_idletasks()
def close_progressbar(win):
win[0].destroy()
def show_message(title, msg):
messagebox.showinfo(title, msg)
def clear_dir(path):
try:
shutil.rmtree(path)
except:
pass