-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkDispModWidget.py
133 lines (113 loc) · 4.1 KB
/
tkDispModWidget.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
from Tkinter import *
from PIL import Image, ImageTk, ImageChops, ImageOps
class tkDispModWidget(Frame):
class ImageSoi:
def __init__(self):
self.imagefile = 'notexture.jpg'
self.rotationflip = 0
self.offx = 0
self.offy = 0
self.scale = float()
self.scale = 1
self.recalcimage()
def recalcimage(self):
self.image = Image.open(self.imagefile)
self.image = self.applyflip(self.image)
bbox = self.image.getbbox()
scalex = int(bbox[2] * self.scale)
scaley = int(bbox[3] * self.scale)
self.image = self.image.resize((scalex, scaley), Image.BICUBIC)
self.image = ImageChops.offset(self.image, self.offx, self.offy)
bbox = self.image.getbbox()
croptuple = (0, bbox[3] - 64, 64, bbox[2])
self.image = self.image.crop(croptuple)
self.image = self.image.resize((100, 100), Image.BICUBIC)
self.image = ImageTk.PhotoImage(self.image)
def applyflip(self, image):
if self.rotationflip == 0:
return image
elif self.rotationflip == 1:
return image.rotate(90)
elif self.rotationflip == 2:
return image.rotate(180)
elif self.rotationflip == 3:
return image.rotate(270)
elif self.rotationflip == 4:
return ImageOps.mirror(image)
elif self.rotationflip == 5:
return ImageOps.flip(image)
def set(self, **options):
if "imagefile" in options.keys():
self.imagefile = options["imagefile"]
if "rotationflip" in options.keys():
self.rotationflip = options["rotationflip"]
if "offx" in options.keys():
self.offx = options["offx"]
if "offy" in options.keys():
self.offy = options["offy"]
if "scale" in options.keys():
self.scale = options["scale"]
if options != {}:
self.recalcimage()
def __init__(self, parent, **options):
Frame.__init__(self, parent, options)
self.imagefile = 'notexture.jpg'
self.rotationflip = 0
self.draw_widget()
def draw_widget(self):
self.displaceframe = Frame(self, width = 144, borderwidth=1)
self.rotdispframe = Frame(self.displaceframe, width = 80, height = 80)
self.dispmodcanv = Canvas(self.rotdispframe, height = 100, width = 100)
self.image = self.ImageSoi()
self.dispmodimg = self.dispmodcanv.create_image( 0, 0, image=self.image.image, anchor=NW)
self.dispmodcanv.grid(row = 1, column = 2,columnspan=1, sticky = 'snew')
self.scalex = Scale(self.rotdispframe, orient=HORIZONTAL, showvalue = 0, from_=0, to=64, command=self.onmodcommand)
self.scalex.grid(row = 2, column = 2, sticky = 'snew')
self.scaley = Scale(self.rotdispframe, orient=VERTICAL, showvalue = 0, from_=64, to=0, command=self.onmodcommand)
self.scaley.grid(row = 1, column = 1, sticky = 'snew')
self.scalet = Scale(self.rotdispframe, orient=VERTICAL, showvalue = 1, from_=4, to=0.05, resolution=.05, command=self.onmodcommand)
self.scalet.set(1)
self.scalet.grid(row = 1, column = 3, rowspan= 2, sticky = 'wens')
self.rotdispframe.grid(row = 2, column = 1, sticky = 'snew')
self.displaceframe.pack()
def onmodcommand(self, value):
self.image.set(imagefile=self.imagefile, offx=self.scalex.get(), offy=self.scaley.get(), scale=self.scalet.get(), rotationflip=self.rotationflip)
self.dispmodcanv.itemconfig(self.dispmodimg, image=self.image.image)
self.command()
def config(self, **options):
if "command" in options.keys():
self.command = options["command"]
if "imagefile" in options.keys():
self.imagefile = options["imagefile"]
if options["imagefile"] == "":
self.imagefile = 'notexture.jpg'
if "rotationflip" in options.keys():
self.rotationflip = options["rotationflip"]
if "offx" in options.keys():
self.scalex.set(options["offx"])
if "offy" in options.keys():
self.scaley.set(options["offy"])
if "scale" in options.keys():
self.scalet.set(options["scale"])
if options != {}:
self.onmodcommand(0)
def get(self):
offx=self.scalex.get()
offy=self.scaley.get()
scale=self.scalet.get()
return (offx, offy, scale)
#class App:
#
# def __init__(self, master):
#
# frame = Frame(master)
# frame.pack()
#
# f = tkDispModWidget(frame)
# f.config(test="Blah", var1="bulge")
# f.pack()
#
#root = Tk()
#
#app = App(root)
#root.mainloop()