-
Notifications
You must be signed in to change notification settings - Fork 21
/
ImageWindow.py
executable file
·101 lines (78 loc) · 2.78 KB
/
ImageWindow.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
#!/usr/bin/env python
"""
ImageWindow.py
A wx component for a resazable window that holds an image
"""
import wx
class ImageWindow(wx.Window):
"""
ImageWindow(Image, *args, **kwargs)
Image: A wx.Image
*args and **kwargs are passed in to wx.Window
"""
def __init__(self, Image, *args, **kwargs):
wx.Window.__init__(self, *args, **kwargs)
self.Image = Image
self._buffer = None
self.Proportional = True
self.BackgroundColor = "Blue"
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_ERASE_BACKGROUND, lambda evt: None)
def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.DrawBitmap(self._buffer, 0, 0)
def OnSize(self, event):
w, h = self.GetSize()
if not self.Proportional:
# scale the image to the new window size
Img = self.Image.Scale(w, h)
self._buffer = wx.BitmapFromImage(Img)
else:
# scale the image, preserving the aspect ratio
iw = self.Image.GetWidth()
ih = self.Image.GetHeight()
if float(iw) / ih > float(w) / h:
NewW = w
NewH = w * ih / iw
else:
NewH = h
NewW = h * iw / ih
Img = self.Image.Scale(NewW,NewH)
# now build the buffer
self._buffer = wx.EmptyBitmap(w, h)
dc = wx.MemoryDC()
dc.SelectObject(self._buffer)
dc.SetBackground(wx.Brush(self.BackgroundColor))
dc.Clear()
x = (w - NewW) / 2
y = (h - NewH) / 2
dc.DrawBitmap(wx.BitmapFromImage(Img), x, y)
self.Refresh(False)
if __name__ == "__main__":
import TestImage
class TestFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
# there needs to be an image here:
Image = TestImage.getImage()
# Using a Sizer to handle the layout: I never like to use absolute postioning
box = wx.BoxSizer(wx.VERTICAL)
# create the first ImageWindow
IW = ImageWindow(Image, self)
IW.Proportional = False
box.Add(IW, 1, wx.ALL | wx.EXPAND, 10)
# create the second ImageWindow
IW = ImageWindow(Image, self)
IW.Proportional = True
IW.BackgroundColor = "Red"
box.Add(IW, 1, wx.ALL | wx.EXPAND, 10)
self.SetSizer(box)
class App(wx.App):
def OnInit(self):
frame = TestFrame(None, title="ImageWindow Test", size=(300, 300))
self.SetTopWindow(frame)
frame.Show(True)
return True
app = App(False)
app.MainLoop()