-
Notifications
You must be signed in to change notification settings - Fork 1
/
import_images.py
65 lines (45 loc) · 1.67 KB
/
import_images.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
from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageTk
from os import walk, makedirs
from os.path import join, exists
import shutil
from settings import SHAPES
SIZE = 500
class App:
def __init__(self):
self.window = Tk()
self.load_dir = filedialog.askdirectory()
(_, _, filenames) = next(walk(self.load_dir))
self.filenames = filenames
self.label = Label(self.window, width=SIZE, height=SIZE)
self.label.pack()
button_frame = Frame(self.window)
button_frame.pack()
for shape in SHAPES:
button = Button(button_frame, text=shape, command=lambda shape=shape : self.copy_image(shape))
button.pack(side=LEFT, padx=10)
shape_dir = join('training', shape)
if not exists(shape_dir):
makedirs(shape_dir)
self.update_image()
self.window.mainloop()
def update_image(self):
img = Image.open(self.cur_path())
width_ratio = 500 / img.width
height_ratio = 500 /img.height
ratio = min(width_ratio, height_ratio)
img = img.resize((int(img.width * ratio), int(img.height * ratio)))
self.image = ImageTk.PhotoImage(img)
self.label.configure(image=self.image)
def copy_image(self, shape):
new_path = join('training', shape, self.filenames[0])
shutil.copyfile(self.cur_path(), new_path)
if len(self.filenames) > 1:
self.filenames.pop(0)
self.update_image()
else:
self.window.destroy()
def cur_path(self):
return join(self.load_dir, self.filenames[0])
App()