[go: up one dir, main page]

Menu

[r860]: / Segtool / SegmentTool.py  Maximize  Restore  History

Download this file

224 lines (175 with data), 7.6 kB

  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
#!/usr/bin/env python
modules = {'DrawingArea' : [0, '', 'none://DrawingArea.py' ],
'Section' : [0, '', 'none://Section.py' ]
}
import wx
import os
from lxml import etree
from DrawingArea import DrawingArea
class MainWindow (wx.Frame):
def __init__ (self, parent, id, title):
wx.Frame.__init__(self,parent,id, title, size=(600,600))
self.CreateMenu()
self.CreateToolbar()
self.images = []
self.shapes = []
self.files = []
self.CreateDisplay()
self.nimages = 0
self.current_image_nr = 0
self.current_set = None
def CreateMenu(self):
menuBar = wx.MenuBar()
Filemenu = wx.Menu()
FileOpenSet = Filemenu.Append(wx.ID_NEW, "&New Set")
FileOpenSet = Filemenu.Append(wx.ID_OPEN, "&Open Set")
FileSaveSet = Filemenu.Append(wx.ID_SAVE, "&Save Set")
FileSaveSetAs = Filemenu.Append(wx.ID_SAVEAS, "Save Set &As")
Filemenu.AppendSeparator()
FileOpenImage = Filemenu.Append(-1, "Import &Images")
FileSaveShape = Filemenu.Append(-1, "Save Single Sha&pe")
Filemenu.AppendSeparator()
FileExit = Filemenu.Append(wx.ID_EXIT, "E&xit")
menuBar.Append(Filemenu, "&File")
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.OnFileExit, FileExit)
self.Bind(wx.EVT_MENU, self.OnFileOpenImage, FileOpenImage)
self.Bind(wx.EVT_MENU, self.OnFileSaveShape, FileSaveShape)
self.Bind(wx.EVT_MENU, self.OnFileOpenSet, FileOpenSet)
self.Bind(wx.EVT_MENU, self.OnFileSaveSet, FileSaveSet)
self.Bind(wx.EVT_MENU, self.OnFileSaveSetAs, FileSaveSetAs)
def CreateToolbar(self):
ap = wx.ArtProvider()
toolbar = self.CreateToolBar()
toolbar.AddSimpleTool(wx.ID_NEW, ap.GetBitmap(wx.ART_NEW, wx.ART_TOOLBAR), \
"New", "Create a new working set")
toolbar.AddSimpleTool(wx.ID_OPEN, ap.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR),\
"Open", "Open a new working set")
toolbar.AddSimpleTool(wx.ID_SAVE, ap.GetBitmap(wx.ART_FILE_SAVE, wx.ART_TOOLBAR), \
"Save", "Save the current working set")
toolbar.AddSimpleTool(wx.ID_SAVEAS, ap.GetBitmap(wx.ART_FILE_SAVE_AS, wx.ART_TOOLBAR), \
"Save", "Save the current working set")
toolbar.AddSeparator()
toolbar.AddSimpleTool(wx.ID_EXIT, ap.GetBitmap(wx.ART_QUIT, wx.ART_TOOLBAR),\
"Exit", "Exit program")
toolbar.Realize()
def CreateDisplay(self):
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.ImageNumSelect = wx.Slider(self, 0, 0, 0, 1, \
style=wx.SL_LABELS|wx.SL_VERTICAL)
self.ImageNumSelect.SetPageSize(1)
self.ImageNumSelect.SetMinSize((16,512))
sizer.Add(self.ImageNumSelect, flag=wx.ALIGN_LEFT)
self.Bind(wx.EVT_SLIDER, self.OnChangeImage, self.ImageNumSelect)
self.da = DrawingArea(self)
self.da.SetMinSize((800,800))
sizer.Add(self.da, flag=wx.ALIGN_LEFT)
self.SetSizer(sizer)
self.Fit()
def OnChangeImage(self, event):
image_nr = self.ImageNumSelect.GetValue()
if image_nr < self.nimages:
self.shapes[ self.current_image_nr ] = self.da.GetSections()
self.da.SetSections( self.shapes[image_nr] )
self.da.SetCurrentBitmap(self.images[image_nr] )
self.current_image_nr = image_nr
def OnFileExit(self, event):
self.Close(True)
def OnFileOpenImage(self, event):
wildcard = "PNG Images (*.png)|*.png|All Files (*.*)|*.*"
flags = wx.OPEN | wx.MULTIPLE
files = self.FileOpen("Open Image files", wildcard, True)
if not files:
return
self.images = []
for f in files:
try:
image = wx.Image(f, type=wx.BITMAP_TYPE_ANY, index = -1)
self.images.append(image)
self.files.append(f)
shape = []
self.shapes.append(shape)
except:
print "unable to read image from %s" % f
self.nimages = len(self.images)
print "loaded %d images" % (self.nimages)
self.ImageNumSelect.SetRange(0, self.nimages - 1)
if self.nimages > 0:
self.da.SetCurrentBitmap(self.images[0])
def FileOpen(self, message, wildcard, multiple):
if multiple:
flags = wx.OPEN |wx.MULTIPLE
else:
flags = wx.OPEN
dialog = wx.FileDialog(self, message, os.getcwd(),"", wildcard, flags)
if dialog.ShowModal() == wx.ID_OK:
if multiple:
files = dialog.GetPaths()
else:
files = dialog.GetPath()
else:
files = None
dialog.Destroy()
return files
def OnFileOpenSet(self, event):
wildcard = "Workset (*.set)|*.set|All Files (*.*)|*.*"
file = self.FileOpen("Open Working set", wildcard, False)
print file
def SaveFrame(self, frame, i):
name = self.files[i]
img = self.images[i]
size = self.da.GetClientSize()
image_name = etree.Element("image", name=name)
frame.append( image_name )
wscale = (img.GetWidth() * 1.0) / size.width
hscale = (img.GetHeight() * 1.0) / size.height
shape = self.shapes[i]
for sections in shape:
section = etree.Element("section")
sections.Save( section, wscale, hscale )
frame.append( section )
def SaveSet(self):
root = etree.Element("workset")
for i in range(len(self.images)):
frame = etree.Element("frame")
self.SaveFrame(frame, i)
root.append(frame)
file = open(self.current_set, "w")
file.write(etree.tostring(root, pretty_print=True))
file.close()
def OnFileSaveSet(self, event):
if not self.current_set:
self.OnFileSaveSetAs(self, event)
else:
self.SaveSet()
def OnFileSaveSetAs(self, event):
wildcard = "Workset Images (*.set)|*.set"
flags = wx.SAVE
dialog = wx.FileDialog(self, "Save Workset", os.getcwd(),"", wildcard, wx.SAVE)
if dialog.ShowModal() == wx.ID_OK:
self.current_set = dialog.GetPath()
if self.current_set[-4:0]!= ".set":
self.current_set = self.current_set + ".set"
self.SaveSet()
dialog.Destroy()
def OnFileSaveShape(self, event):
name = self.files[self.current_image_nr]
img = self.images[self.current_image_nr]
size = self.da.GetClientSize()
wscale = (img.GetWidth() * 1.0) / size.width
hscale = (img.GetHeight() * 1.0) / size.height
root = etree.Element("frame")
self.SaveFrame(root, self.current_image_nr)
result = etree.tostring(root, pretty_print=True)
file = open(name + ".xml", "w")
file.write(result)
file.close()
class App(wx.App):
def OnInit(self):
self.frame = MainWindow(parent=None, id=-1, title='Segment')
self.frame.Show()
self.SetTopWindow(self.frame)
return True
if __name__ == '__main__':
app = App()
app.MainLoop()