-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
371 lines (358 loc) · 14 KB
/
main.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import cache
import core
import os
import wx
import platform
import subprocess
import urllib2
IMAGES_PATH = os.path.abspath('images')
def show_folder(path):
system = platform.system().lower()
if system == 'darwin':
try:
from appscript import App, mactypes
if os.path.exists(path):
App("Finder").reveal(mactypes.Alias(path).alias)
except:
subprocess.call(['open', '-R', path])
elif system == 'windows':
if os.path.exists(IMAGES_PATH):
subprocess.call(['start', path], shell=True)
elif system == 'linux':
print 'Linux not implemented yet.'
def menu_item(window, menu, label, func):
item = wx.MenuItem(menu, -1, label)
menu.AppendItem(item)
if func:
window.Bind(wx.EVT_MENU, func, id=item.GetId())
return item
def tool_item(window, toolbar, label, func, icon):
item = toolbar.AddSimpleTool(-1, wx.Bitmap(icon), label)
if func:
window.Bind(wx.EVT_TOOL, func, id=item.GetId())
return item
def load_images():
result = []
names = os.listdir(IMAGES_PATH)
for name in names:
base, ext = os.path.splitext(name)
if ext in ('.png', '.jpg', '.jpeg'):
title = base.replace('-', ' ')
title = title.replace('_', ' ')
full_path = os.path.join(IMAGES_PATH, name)
result.append((title, full_path))
return result
class ImageDropTarget(wx.PyDropTarget):
def __init__(self, callback):
super(ImageDropTarget, self).__init__()
self.callback = callback
self.do = wx.DataObjectComposite()
self.file_do = wx.FileDataObject()
self.url_do = wx.TextDataObject()
self.do.Add(self.file_do)
self.do.Add(self.url_do)
self.SetDataObject(self.do)
def OnData(self, x, y, data):
if self.GetData():
df = self.do.GetReceivedFormat().GetType()
if df in [wx.DF_UNICODETEXT, wx.DF_TEXT]:
try:
self.callback(self.url_do.GetText())
except:
pass
elif df == wx.DF_FILENAME:
try:
self.callback(self.file_do.GetFilenames()[0])
except:
pass
class Model(object):
def __init__(self):
self.path = ''
self.reset()
def reset(self):
self.header = ''
self.footer = ''
self.header_size = 20
self.footer_size = 20
self.header_alignment = core.CENTER
self.footer_alignment = core.CENTER
self.padding = 10
self.border_size = 3
def generate(self):
background = cache.get_bitmap(self.path)
width, height = background.GetSize()
page = core.Page()
page.add(core.Bitmap(background))
text_width = width - self.padding * 2
adjusted_header_size = int(height*(self.header_size/200.0))
adjusted_footer_size = int(height*(self.footer_size/200.0))
header = core.Text(
self.header.upper(),
text_width,
alignment=self.header_alignment,
border_color=(0, 0, 0),
border_size=self.border_size,
color=(255, 255, 255),
font=core.font('Impact', adjusted_header_size),
)
page.add(header, (width / 2, self.padding), (0.5, 0))
footer = core.Text(
self.footer.upper(),
text_width,
alignment=self.footer_alignment,
border_color=(0, 0, 0),
border_size=self.border_size,
color=(255, 255, 255),
font=core.font('Impact', adjusted_footer_size),
)
page.add(footer, (width / 2, -self.padding), (0.5, 1))
bitmap = wx.EmptyBitmap(width, height)
dc = wx.MemoryDC(bitmap)
page.render(dc, (width, height), (0, 0))
return bitmap
class BitmapView(wx.Panel):
def __init__(self, parent):
super(BitmapView, self).__init__(parent, style=wx.BORDER_STATIC)
self.bitmap = None
self.scaled_bitmap = None
self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
self.Bind(wx.EVT_PAINT, self.on_paint)
self.Bind(wx.EVT_SIZE, self.on_size)
def set_bitmap(self, bitmap):
self.bitmap = bitmap
self.Refresh()
def on_size(self, event):
event.Skip()
self.Refresh()
def on_paint(self, event):
color = wx.SystemSettings_GetColour(wx.SYS_COLOUR_BTNFACE)
dc = wx.BufferedPaintDC(self)
dc.SetBackground(wx.Brush(color))
dc.Clear()
dc = wx.GCDC(dc)
if self.bitmap is None:
return
pad = 0
cw, ch = self.GetClientSize()
dw, dh = cw - pad * 2, ch - pad * 2
bw, bh = self.bitmap.GetSize()
xr, yr = float(dw) / float(bw), float(dh) / float(bh)
ratio = min(xr, yr)
ratio = min(ratio, 1)
sw, sh = int(cw / ratio), int(ch / ratio)
x, y = (sw - bw) / 2, (sh - bh) / 2
dc.SetUserScale(ratio, ratio)
dc.DrawBitmap(self.bitmap, x, y)
class Frame(wx.Frame):
def __init__(self):
super(Frame, self).__init__(None, -1, 'iMeme')
self.SetIcon(wx.Icon('icons/icon.ico', wx.BITMAP_TYPE_ICO))
self.model = Model()
self.create_menu()
self.create_toolbar()
panel = self.create_contents(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(panel, 1, wx.EXPAND)
self.SetSizerAndFit(sizer)
self.on_change()
def on_change(self):
self.bitmap_view.set_bitmap(self.model.generate())
def on_files_dropped(self, data):
if data:
self.model.path = data
self.on_change()
def create_contents(self, parent):
panel = wx.Panel(parent)
controls = self.create_controls(panel)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(controls, 1, wx.EXPAND | wx.ALL, 12)
panel.SetSizerAndFit(sizer)
return panel
def create_controls(self, parent):
list_box = self.create_list_box(parent)
right = self.create_right(parent)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(list_box, 0, wx.EXPAND)
sizer.AddSpacer(8)
sizer.Add(right, 1, wx.EXPAND)
return sizer
def create_list_box(self, parent):
self.list_box = wx.ListBox(parent, size=(200, -1))
self.list_box.Bind(wx.EVT_LISTBOX, self.on_list_box)
data = load_images()
for title, path in data:
self.list_box.Append(title, path)
self.model.path = data[0][1]
self.list_box.Select(0)
return self.list_box
def create_right(self, parent):
view = self.create_view(parent)
widgets = self.create_widgets(parent)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(view, 1, wx.EXPAND)
sizer.AddSpacer(8)
sizer.Add(widgets, 0, wx.EXPAND)
return sizer
def create_view(self, parent):
self.bitmap_view = BitmapView(parent)
self.bitmap_view.SetMinSize((400, 400))
self.bitmap_view.SetDropTarget(ImageDropTarget(self.on_files_dropped))
return self.bitmap_view
def create_widgets(self, parent):
self.header = header = wx.TextCtrl(parent)
self.footer = footer = wx.TextCtrl(parent)
header_smaller = wx.BitmapButton(parent, -1, wx.Bitmap('icons/minus.png'))
header_bigger = wx.BitmapButton(parent, -1, wx.Bitmap('icons/plus.png'))
header_left = wx.BitmapButton(parent, -1, wx.Bitmap('icons/align-left.png'))
header_center = wx.BitmapButton(parent, -1, wx.Bitmap('icons/align-center.png'))
header_right = wx.BitmapButton(parent, -1, wx.Bitmap('icons/align-right.png'))
footer_smaller = wx.BitmapButton(parent, -1, wx.Bitmap('icons/minus.png'))
footer_bigger = wx.BitmapButton(parent, -1, wx.Bitmap('icons/plus.png'))
footer_left = wx.BitmapButton(parent, -1, wx.Bitmap('icons/align-left.png'))
footer_center = wx.BitmapButton(parent, -1, wx.Bitmap('icons/align-center.png'))
footer_right = wx.BitmapButton(parent, -1, wx.Bitmap('icons/align-right.png'))
header.Bind(wx.EVT_TEXT, self.on_header)
header_smaller.Bind(wx.EVT_BUTTON, self.on_header_smaller)
header_bigger.Bind(wx.EVT_BUTTON, self.on_header_bigger)
header_left.Bind(wx.EVT_BUTTON, self.on_header_left)
header_center.Bind(wx.EVT_BUTTON, self.on_header_center)
header_right.Bind(wx.EVT_BUTTON, self.on_header_right)
footer.Bind(wx.EVT_TEXT, self.on_footer)
footer_smaller.Bind(wx.EVT_BUTTON, self.on_footer_smaller)
footer_bigger.Bind(wx.EVT_BUTTON, self.on_footer_bigger)
footer_left.Bind(wx.EVT_BUTTON, self.on_footer_left)
footer_center.Bind(wx.EVT_BUTTON, self.on_footer_center)
footer_right.Bind(wx.EVT_BUTTON, self.on_footer_right)
grid = wx.FlexGridSizer(2, 8, 8, 0)
grid.AddGrowableCol(0)
grid.Add(header, flag=wx.EXPAND)
grid.AddSpacer(8)
grid.Add(header_smaller)
grid.Add(header_bigger)
grid.AddSpacer(8)
grid.Add(header_left)
grid.Add(header_center)
grid.Add(header_right)
grid.Add(footer, flag=wx.EXPAND)
grid.AddSpacer(8)
grid.Add(footer_smaller)
grid.Add(footer_bigger)
grid.AddSpacer(8)
grid.Add(footer_left)
grid.Add(footer_center)
grid.Add(footer_right)
return grid
def create_menu(self):
menubar = wx.MenuBar()
# File
menu = wx.Menu()
menu_item(self, menu, 'New\tCtrl+N', self.on_new)
menu_item(self, menu, 'Open...\tCtrl+O', self.on_open)
menu_item(self, menu, 'Save As...\tCtrl+S', self.on_save)
menu.AppendSeparator()
menu_item(self, menu, 'Show Images Folder', self.on_show_images)
menu.AppendSeparator()
menu_item(self, menu, 'Exit\tAlt+F4', self.on_exit)
menubar.Append(menu, '&File')
self.SetMenuBar(menubar)
def create_toolbar(self):
toolbar = self.CreateToolBar()
toolbar.SetToolBitmapSize((18, 18))
tool_item(self, toolbar, 'New', self.on_new, 'icons/new.png')
tool_item(self, toolbar, 'Open', self.on_open, 'icons/open.png')
tool_item(self, toolbar, 'Save', self.on_save, 'icons/save.png')
toolbar.Realize()
toolbar.Fit()
self.SetToolBar(toolbar)
def on_list_box(self, event):
index = self.list_box.GetSelection()
if index >= 0:
self.model.path = self.list_box.GetClientData(index)
self.on_change()
def on_header(self, event):
self.model.header = self.header.GetValue()
self.on_change()
def on_header_smaller(self, event):
if self.model.header_size <= 10:
self.model.header_size -= 1
else:
self.model.header_size -= 4
self.model.header_size = max(self.model.header_size, 1)
self.on_change()
def on_header_bigger(self, event):
if self.model.header_size <= 10:
self.model.header_size += 1
else:
self.model.header_size += 4
self.model.header_size = min(self.model.header_size, 100)
self.on_change()
def on_header_left(self, event):
self.model.header_alignment = core.LEFT
self.on_change()
def on_header_center(self, event):
self.model.header_alignment = core.CENTER
self.on_change()
def on_header_right(self, event):
self.model.header_alignment = core.RIGHT
self.on_change()
def on_footer(self, event):
self.model.footer = self.footer.GetValue()
self.on_change()
def on_footer_smaller(self, event):
if self.model.footer_size <= 10:
self.model.footer_size -= 1
else:
self.model.footer_size -= 4
self.model.footer_size = max(self.model.footer_size, 1)
self.on_change()
def on_footer_bigger(self, event):
if self.model.footer_size <= 10:
self.model.footer_size += 1
else:
self.model.footer_size += 4
self.model.footer_size = min(self.model.footer_size, 100)
self.on_change()
def on_footer_left(self, event):
self.model.footer_alignment = core.LEFT
self.on_change()
def on_footer_center(self, event):
self.model.footer_alignment = core.CENTER
self.on_change()
def on_footer_right(self, event):
self.model.footer_alignment = core.RIGHT
self.on_change()
def on_new(self, event):
self.model.reset()
self.header.SetValue('')
self.footer.SetValue('')
self.on_change()
def on_open(self, event):
exts = sorted(['*.png', '*.jpg', '*.jpeg', '*.gif', '*.bmp'])
wildcard = ';'.join(exts)
dialog = wx.FileDialog(self, 'Open', wildcard=wildcard,
style=wx.FD_OPEN|wx.FD_FILE_MUST_EXIST)
if dialog.ShowModal() == wx.ID_OK:
path = dialog.GetPath()
self.model.path = path
self.on_change()
def on_save(self, event):
dialog = wx.FileDialog(self, 'Save As', wildcard='*.jpg',
style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
if dialog.ShowModal() == wx.ID_OK:
path = dialog.GetPath()
bitmap = self.model.generate()
image = wx.ImageFromBitmap(bitmap)
image.SetOptionInt(wx.IMAGE_OPTION_QUALITY, 95)
image.SaveFile(path, wx.BITMAP_TYPE_JPEG)
def on_show_images(self, event):
show_folder(IMAGES_PATH)
def on_exit(self, event):
self.Close()
def main():
app = wx.PySimpleApp()
frame = Frame()
frame.Center()
frame.Show()
app.MainLoop()
if __name__ == '__main__':
main()