1 #----------------------------------------------------------------------------
3 # Purpose: Display and Select Image Files
8 # Date: August 15, 2001
9 # Licence: wxWindows license
10 #----------------------------------------------------------------------------
12 import os
, sys
, string
13 from wxPython
.wx
import *
14 dir_path
= os
.getcwd()
16 #---------------------------------------------------------------------------
18 def ConvertBMP(file_nm
):
22 fl_fld
= os
.path
.splitext(file_nm
)
24 ext
= string
.lower(ext
[1:])
26 image
= wxImage(file_nm
, wxBITMAP_TYPE_BMP
)
28 image
= wxImage(file_nm
, wxBITMAP_TYPE_GIF
)
30 image
= wxImage(file_nm
, wxBITMAP_TYPE_PNG
)
32 image
= wxImage(file_nm
, wxBITMAP_TYPE_JPEG
)
38 def GetSize(file_nm
): # for scaling image values
39 image
= ConvertBMP(file_nm
)
40 bmp
= image
.ConvertToBitmap()
41 size
= bmp
.GetWidth(), bmp
.GetHeight()
44 class ImageView(wxWindow
):
45 def __init__(self
, parent
, id=-1, pos
=wxDefaultPosition
, size
=wxDefaultSize
):
46 wxWindow
.__init
__(self
, parent
, id, pos
, size
)
49 self
.back_color
= 'WHITE'
50 self
.border_color
= 'BLACK'
52 self
.image_sizex
= size
.width
53 self
.image_sizey
= size
.height
54 self
.image_posx
= pos
.x
55 self
.image_posy
= pos
.y
56 EVT_PAINT(self
, self
.OnPaint
)
58 wxInitAllImageHandlers()
60 def OnPaint(self
, event
):
64 def DrawImage(self
, dc
):
69 def SetValue(self
, file_nm
): # display the selected file in the panel
70 image
= ConvertBMP(file_nm
)
74 def DrawBorder(self
, dc
):
75 brush
= wxBrush(wxNamedColour(self
.back_color
), wxSOLID
)
77 dc
.SetPen(wxPen(wxNamedColour(self
.border_color
), 1))
78 dc
.DrawRectangle(0, 0, self
.image_sizex
, self
.image_sizey
)
80 def DrawImage(self
, dc
):
90 bmp
= image
.ConvertToBitmap()
92 iwidth
= bmp
.GetWidth() # dimensions of image file
93 iheight
= bmp
.GetHeight()
95 diffx
= (self
.image_sizex
- iwidth
)/2 # center calc
96 if iwidth
>= self
.image_sizex
-10: # if image width fits in window adjust
98 iwidth
= self
.image_sizex
- 10
100 diffy
= (self
.image_sizey
- iheight
)/2 # center calc
101 if iheight
>= self
.image_sizey
- 10: # if image height fits in window adjust
103 iheight
= self
.image_sizey
- 10
105 image
.Rescale(iwidth
, iheight
) # rescale to fit the window
106 image
.ConvertToBitmap()
107 bmp
= image
.ConvertToBitmap()
108 dc
.DrawBitmap(bmp
, diffx
, diffy
) # draw the image to window
111 class ImageDialog(wxDialog
):
112 def __init__(self
, parent
, set_dir
= None):
113 wxDialog
.__init
__(self
, parent
, -1, "Image Browser", wxPyDefaultPosition
, wxSize(400, 400))
115 self
.x_pos
= 30 # initial display positions
119 size
= wxSize(80, 25)
121 self
.set_dir
= os
.getcwd()
124 if os
.path
.exists(set_dir
): # set to working directory if nothing set
125 self
.set_dir
= set_dir
127 self
.dir_x
= self
.x_pos
128 self
.dir_y
= self
.y_pos
129 self
.DisplayDir() # display the directory value
131 self
.y_pos
= self
.y_pos
+ self
.delta
134 wxButton(self
, mID
, ' Set Directory ', wxPoint(self
.x_pos
, self
.y_pos
), size
).SetDefault()
135 EVT_BUTTON(self
, mID
, self
.SetDirect
)
137 self
.type_posy
= self
.y_pos
# save the y position for the image type combo
139 self
.fl_ext
= '*.bmp' # initial setting for file filtering
140 self
.GetFiles() # get the file list
142 self
.y_pos
= self
.y_pos
+ self
.delta
+ 10
144 self
.list_height
= 150
148 self
.tb
= tb
= wxListBox(self
, mID
, wxPoint(self
.x_pos
, self
.y_pos
), wxSize(160, self
.list_height
), self
.fl_list
, wxLB_SINGLE
)
149 EVT_LISTBOX(self
, mID
, self
.OnListClick
)
150 EVT_LISTBOX_DCLICK(self
, mID
, self
.OnListDClick
)
152 width
, height
= self
.tb
.GetSizeTuple()
153 image_posx
= self
.x_pos
+ width
+ 20 # positions for setting the image window
154 image_posy
= self
.y_pos
156 image_sizey
= self
.list_height
158 self
.fl_types
= ["Bmp", "Gif", "Png", "Jpg"]
159 self
.fl_ext_types
= { "Bmp": "*.bmp", "Gif": "*.gif", "Png": "*.png", "Jpg": "*.jpg" }
161 self
.set_type
= self
.fl_types
[0] # initial file filter setting
164 self
.sel_type
= wxComboBox(self
, mID
, self
.set_type
, wxPoint(image_posx
, self
.type_posy
), wxSize(150, -1), self
.fl_types
, wxCB_DROPDOWN
)
165 EVT_COMBOBOX(self
, mID
, self
.OnSetType
)
167 self
.image_view
= ImageView(self
, pos
=wxPoint(image_posx
, image_posy
), size
=wxSize(image_sizex
, image_sizey
))
169 self
.y_pos
= self
.y_pos
+ height
+ 20
172 wxButton(self
, mID
, ' Select ', wxPoint(100, self
.y_pos
), size
).SetDefault()
173 EVT_BUTTON(self
, mID
, self
.OnOk
)
175 wxButton(self
, wxID_CANCEL
, 'Cancel', wxPoint(250, self
.y_pos
), size
)
177 self
.y_pos
= self
.y_pos
+ self
.delta
178 fsize
= wxSize(400, self
.y_pos
+ 50) # resize dialog for final vertical position
183 def GetFiles(self
): # get the file list using directory and extension values
184 self
.fl_val
= FindFiles(self
, self
.set_dir
, self
.fl_ext
)
185 self
.fl_list
= self
.fl_val
.files
187 def DisplayDir(self
): # display the working directory
188 wxStaticText(self
, -1, self
.set_dir
, wxPoint(self
.dir_x
, self
.dir_y
), wxSize(250, -1))
190 def OnSetType(self
, event
):
191 val
= event
.GetString() # get file type value
192 self
.fl_ext
= self
.fl_ext_types
[val
]
195 def OnListDClick(self
, event
):
198 def OnListClick(self
, event
):
199 val
= event
.GetSelection()
200 self
.SetListValue(val
)
202 def SetListValue(self
, val
):
203 file_nm
= self
.fl_list
[val
]
204 self
.set_file
= file_val
= os
.path
.join(self
.set_dir
, file_nm
)
205 self
.image_view
.SetValue(file_val
)
207 def SetDirect(self
, event
): # set the new directory
208 dlg
= wxDirDialog(self
)
209 dlg
.SetPath(self
.set_dir
)
210 if dlg
.ShowModal() == wxID_OK
:
211 self
.set_dir
= dlg
.GetPath()
215 def ResetFiles(self
): # refresh the display with files and initial image
218 self
.tb
.Set(self
.fl_list
)
220 self
.tb
.SetSelection(0)
223 self
.image_view
.SetValue(None)
228 def GetDirectory(self
):
231 def OnCancel(self
, event
):
233 self
.EndModal(wxID_CANCEL
)
235 def OnOk(self
, event
):
236 self
.result
= self
.set_file
237 self
.EndModal(wxID_OK
)
241 dlg
= wxFileDialog(self
, "Choose an Image File", ".", "", "Bmp (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg", wxOPEN
)
242 if dlg
.ShowModal() == wxID_OK
:
250 def __init__(self
, parent
, dir, mask
):
255 mask
= string
.upper(mask
)
257 for i
in os
.listdir(dir):
258 if i
== "." or i
== "..":
260 path
= os
.path
.join(dir, i
)
261 path
= string
.upper(path
)
262 value
= string
.upper(i
)
264 if self
.regex
.match(value
) == len(value
):
267 self
.files
= filelist
269 def MakeRegex(self
, pattern
):
271 f
= "" # Set up a regex for file names
282 self
.regex
= regex
.compile(f
)
284 def StripExt(self
, file_nm
):
285 fl_fld
= os
.path
.splitext(file_nm
)