2 #----------------------------------------------------------------------------
4 # Purpose: Display and Select Image Files
9 # Date: August 15, 2001
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
13 import os
, sys
, string
14 from wxPython
.wx
import *
15 dir_path
= os
.getcwd()
17 #---------------------------------------------------------------------------
19 def ConvertBMP(file_nm
):
23 fl_fld
= os
.path
.splitext(file_nm
)
25 ext
= string
.lower(ext
[1:])
27 image
= wxImage(file_nm
, wxBITMAP_TYPE_BMP
)
29 image
= wxImage(file_nm
, wxBITMAP_TYPE_GIF
)
31 image
= wxImage(file_nm
, wxBITMAP_TYPE_PNG
)
33 image
= wxImage(file_nm
, wxBITMAP_TYPE_JPEG
)
39 def GetSize(file_nm
): # for scaling image values
40 image
= ConvertBMP(file_nm
)
41 bmp
= image
.ConvertToBitmap()
42 size
= bmp
.GetWidth(), bmp
.GetHeight()
45 class ImageView(wxWindow
):
46 def __init__(self
, parent
, id=-1, pos
=wxDefaultPosition
, size
=wxDefaultSize
):
47 wxWindow
.__init
__(self
, parent
, id, pos
, size
)
50 self
.back_color
= 'WHITE'
51 self
.border_color
= 'BLACK'
53 self
.image_sizex
= size
.width
54 self
.image_sizey
= size
.height
55 self
.image_posx
= pos
.x
56 self
.image_posy
= pos
.y
57 EVT_PAINT(self
, self
.OnPaint
)
59 wxInitAllImageHandlers()
61 def OnPaint(self
, event
):
65 def DrawImage(self
, dc
):
70 def SetValue(self
, file_nm
): # display the selected file in the panel
71 image
= ConvertBMP(file_nm
)
75 def DrawBorder(self
, dc
):
76 brush
= wxBrush(wxNamedColour(self
.back_color
), wxSOLID
)
78 dc
.SetPen(wxPen(wxNamedColour(self
.border_color
), 1))
79 dc
.DrawRectangle(0, 0, self
.image_sizex
, self
.image_sizey
)
81 def DrawImage(self
, dc
):
91 bmp
= image
.ConvertToBitmap()
93 iwidth
= bmp
.GetWidth() # dimensions of image file
94 iheight
= bmp
.GetHeight()
96 diffx
= (self
.image_sizex
- iwidth
)/2 # center calc
97 if iwidth
>= self
.image_sizex
-10: # if image width fits in window adjust
99 iwidth
= self
.image_sizex
- 10
101 diffy
= (self
.image_sizey
- iheight
)/2 # center calc
102 if iheight
>= self
.image_sizey
- 10: # if image height fits in window adjust
104 iheight
= self
.image_sizey
- 10
106 image
.Rescale(iwidth
, iheight
) # rescale to fit the window
107 image
.ConvertToBitmap()
108 bmp
= image
.ConvertToBitmap()
109 dc
.DrawBitmap(bmp
, diffx
, diffy
) # draw the image to window
112 class ImageDialog(wxDialog
):
113 def __init__(self
, parent
, set_dir
= None):
114 wxDialog
.__init
__(self
, parent
, -1, "Image Browser", wxPyDefaultPosition
, wxSize(400, 400))
116 self
.x_pos
= 30 # initial display positions
120 size
= wxSize(80, 25)
122 self
.set_dir
= os
.getcwd()
125 if os
.path
.exists(set_dir
): # set to working directory if nothing set
126 self
.set_dir
= set_dir
128 self
.dir_x
= self
.x_pos
129 self
.dir_y
= self
.y_pos
130 self
.DisplayDir() # display the directory value
132 self
.y_pos
= self
.y_pos
+ self
.delta
135 wxButton(self
, mID
, ' Set Directory ', wxPoint(self
.x_pos
, self
.y_pos
), size
).SetDefault()
136 EVT_BUTTON(self
, mID
, self
.SetDirect
)
138 self
.type_posy
= self
.y_pos
# save the y position for the image type combo
140 self
.fl_ext
= '*.bmp' # initial setting for file filtering
141 self
.GetFiles() # get the file list
143 self
.y_pos
= self
.y_pos
+ self
.delta
+ 10
145 self
.list_height
= 150
149 self
.tb
= tb
= wxListBox(self
, mID
, wxPoint(self
.x_pos
, self
.y_pos
), wxSize(160, self
.list_height
), self
.fl_list
, wxLB_SINGLE
)
150 EVT_LISTBOX(self
, mID
, self
.OnListClick
)
151 EVT_LISTBOX_DCLICK(self
, mID
, self
.OnListDClick
)
153 width
, height
= self
.tb
.GetSizeTuple()
154 image_posx
= self
.x_pos
+ width
+ 20 # positions for setting the image window
155 image_posy
= self
.y_pos
157 image_sizey
= self
.list_height
159 self
.fl_types
= ["Bmp", "Gif", "Png", "Jpg"]
160 self
.fl_ext_types
= { "Bmp": "*.bmp", "Gif": "*.gif", "Png": "*.png", "Jpg": "*.jpg" }
162 self
.set_type
= self
.fl_types
[0] # initial file filter setting
165 self
.sel_type
= wxComboBox(self
, mID
, self
.set_type
, wxPoint(image_posx
, self
.type_posy
), wxSize(150, -1), self
.fl_types
, wxCB_DROPDOWN
)
166 EVT_COMBOBOX(self
, mID
, self
.OnSetType
)
168 self
.image_view
= ImageView(self
, pos
=wxPoint(image_posx
, image_posy
), size
=wxSize(image_sizex
, image_sizey
))
170 self
.y_pos
= self
.y_pos
+ height
+ 20
173 wxButton(self
, mID
, ' Select ', wxPoint(100, self
.y_pos
), size
).SetDefault()
174 EVT_BUTTON(self
, mID
, self
.OnOk
)
176 wxButton(self
, wxID_CANCEL
, 'Cancel', wxPoint(250, self
.y_pos
), size
)
178 self
.y_pos
= self
.y_pos
+ self
.delta
179 fsize
= wxSize(400, self
.y_pos
+ 50) # resize dialog for final vertical position
184 def GetFiles(self
): # get the file list using directory and extension values
185 self
.fl_val
= FindFiles(self
, self
.set_dir
, self
.fl_ext
)
186 self
.fl_list
= self
.fl_val
.files
188 def DisplayDir(self
): # display the working directory
189 wxStaticText(self
, -1, self
.set_dir
, wxPoint(self
.dir_x
, self
.dir_y
), wxSize(250, -1))
191 def OnSetType(self
, event
):
192 val
= event
.GetString() # get file type value
193 self
.fl_ext
= self
.fl_ext_types
[val
]
196 def OnListDClick(self
, event
):
199 def OnListClick(self
, event
):
200 val
= event
.GetSelection()
201 self
.SetListValue(val
)
203 def SetListValue(self
, val
):
204 file_nm
= self
.fl_list
[val
]
205 self
.set_file
= file_val
= os
.path
.join(self
.set_dir
, file_nm
)
206 self
.image_view
.SetValue(file_val
)
208 def SetDirect(self
, event
): # set the new directory
209 dlg
= wxDirDialog(self
)
210 dlg
.SetPath(self
.set_dir
)
211 if dlg
.ShowModal() == wxID_OK
:
212 self
.set_dir
= dlg
.GetPath()
216 def ResetFiles(self
): # refresh the display with files and initial image
219 self
.tb
.Set(self
.fl_list
)
221 self
.tb
.SetSelection(0)
224 self
.image_view
.SetValue(None)
229 def GetDirectory(self
):
232 def OnCancel(self
, event
):
234 self
.EndModal(wxID_CANCEL
)
236 def OnOk(self
, event
):
237 self
.result
= self
.set_file
238 self
.EndModal(wxID_OK
)
242 dlg
= wxFileDialog(self
, "Choose an Image File", ".", "", "Bmp (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg", wxOPEN
)
243 if dlg
.ShowModal() == wxID_OK
:
251 def __init__(self
, parent
, dir, mask
):
256 mask
= string
.upper(mask
)
258 for i
in os
.listdir(dir):
259 if i
== "." or i
== "..":
261 path
= os
.path
.join(dir, i
)
262 path
= string
.upper(path
)
263 value
= string
.upper(i
)
265 if self
.regex
.match(value
) == len(value
):
268 self
.files
= filelist
270 def MakeRegex(self
, pattern
):
272 f
= "" # Set up a regex for file names
283 self
.regex
= regex
.compile(f
)
285 def StripExt(self
, file_nm
):
286 fl_fld
= os
.path
.splitext(file_nm
)