-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
194 lines (155 loc) · 6.57 KB
/
app.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
from Tkinter import Tk, Frame, Button, Label
from tkFileDialog import askopenfilename
from PIL import Image
from PIL.ImageTk import PhotoImage
from key import Key, Share
from watermarking import Timestamp, Marker
from auth import auth
class _Label(Frame):
def __init__(self, master=None, width=0, height=0, **kwargs):
self.width = width
self.height = height
Frame.__init__(self, master, width=self.width, height=self.height)
self.label = Label(self, **kwargs)
self.label.pack(expand=True, fill='both')
self.label.saved_image = None
def pack(self, *args, **kwargs):
Frame.pack(self, *args, **kwargs)
self.pack_propagate(False)
def grid(self, *args, **kwargs):
Frame.grid(self, *args, **kwargs)
self.grid_propagate(False)
@property
def image(self):
return self.label.saved_image
def set_img(self, img):
# img = Image.open(filename)
width = self.width if img.width > self.width else img.width
height = self.height if img.height > self.height else img.height
if width == self.width or height == self.height:
print 'resized'
self_ratio = 1.0 * self.width / self.height
img_ratio = 1.0 * img.width / img.height
height = self.height if self_ratio > img_ratio else img.height
width = self.height * img_ratio
resized = img.resize((int(width), int(height)), Image.ANTIALIAS)
else:
resized = img
resized = PhotoImage(resized)
self.label.configure(image=resized)
self.label.image = resized
self.label.saved_image = img
class App(Frame):
def insert_image(self, panel, filename=None, image=None):
if filename is None and image is None:
raise TypeError('insert_image takes filename or image')
if filename is not None:
panel.set_img(Image.open(filename))
elif image is not None:
panel.set_img(image)
else:
raise TypeError
def key(self):
Key.generate_key()
self.insert_image(self._key_img, filename=Key.FILENAME)
def timestamp(self):
Timestamp.current()
self.insert_image(self._ts_img, filename=Timestamp.FILENAME)
def share(self):
if (self._ts_img.image is not None and
self._key_img.image is not None):
share = Share.make(
self._ts_img.image,
self._key_img.image
)
self.insert_image(self._share_img, filename=Share.FILENAME)
# self.insert_image(self._share_img, image=share)
self._share = share
else:
raise ValueError('share can be made from key and timestamp')
def input_img(self):
filename = askopenfilename()
if filename:
self.insert_image(self._input_img, filename)
def watermark(self):
if (self._share_img.image is not None and
self._input_img.image is not None):
rgb_image = self._input_img.image.convert('RGB')
wm = Marker.watermark(
rgb_image,
self._share
)
self.insert_image(self._watermark_img, filename=Marker.FILENAME)
else:
raise ValueError('share can be made from share and input image')
def verify(self):
if (self._key_img.image is not None and
self._watermark_img.image is not None):
wm = auth(
self._watermark_img.image,
self._key_img.image
)
self.insert_image(self._verify_img, filename='watermark.png')
else:
raise ValueError('share can be made from share and input image')
def create_widgets(self):
self.columnconfigure(0, pad=3)
self.columnconfigure(1, pad=3)
self.columnconfigure(2, pad=3)
self.columnconfigure(3, pad=3)
self.columnconfigure(4, pad=3)
self.columnconfigure(5, pad=3)
self.columnconfigure(6, pad=3)
self.rowconfigure(0, pad=50)
self.rowconfigure(1, pad=50)
self.rowconfigure(2, pad=50)
self.rowconfigure(3, pad=50)
self._key = Button(self, text='key', command=self.key)
self._key.pack(side='left')
self._key.grid(row=0, column=0)
self._key_img = _Label(master=self, width=170, height=30)
self._key_img.pack(side='right', fill='both', expand='no')
self._key_img.grid(row=0, column=1)
self._ts = Button(self, text='timestamp', command=self.timestamp)
self._ts.pack(side='left')
self._ts.grid(row=0, column=2)
self._ts_img = _Label(master=self, width=170, height=30)
self._ts_img.pack(side='right', fill='both', expand='no')
self._ts_img.grid(row=0, column=3)
self._share = Button(self, text='share', command=self.share)
self._share.pack(side='left')
self._share.grid(row=0, column=4)
self._share_img = _Label(master=self, width=170, height=30)
self._share_img.pack(side='right', fill='both', expand='no')
self._share_img.grid(row=0, column=5)
self._input = Button(self, text='image', command=self.input_img)
self._input.pack(side='bottom')
self._input.grid(row=1, column=0)
self._input_img = _Label(master=self, width=770, height=196)
self._input_img.pack(side='right', fill='both', expand='no')
self._input_img.grid(row=1, column=1, columnspan=5)
self._watermark = Button(self,
text='watermark', command=self.watermark)
self._watermark.pack(side='bottom')
self._watermark.grid(row=2, column=0)
self._watermark_img = _Label(master=self, width=770, height=196)
self._watermark_img.pack(side='right', fill='both', expand='no')
self._watermark_img.grid(row=2, column=1, columnspan=5)
self._verify = Button(self, text='verify', command=self.verify)
self._verify.pack(side='bottom')
self._verify.grid(row=3, column=0)
self._verify_img = _Label(master=self, width=770, height=196)
self._verify_img.pack(side='right', fill='both', expand='no')
self._verify_img.grid(row=3, column=1, columnspan=5)
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
self.pack()
self.create_widgets()
if __name__ == '__main__':
root = Tk()
root.title('Watermark')
root.minsize(950, 950)
app = App(master=root, width=900, height=750)
root.mainloop()
closeInput = raw_input("Press ENTER to exit")
print "Closing..."