2 #---------------------------------------------------------------------------- 
   4 # Purpose:      Display and Select Image Files 
  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
): 
  20     fl_fld 
= os
.path
.splitext(file_nm
) 
  22     ext 
= string
.lower(ext
[1:]) 
  24         image 
= wxImage(file_nm
, wxBITMAP_TYPE_BMP
) 
  26         image 
= wxImage(file_nm
, wxBITMAP_TYPE_GIF
) 
  28         image 
= wxImage(file_nm
, wxBITMAP_TYPE_PNG
) 
  30         image 
= wxImage(file_nm
, wxBITMAP_TYPE_JPEG
) 
  37     def __init__(self
, iparent
, ipos
, isize
): 
  39         self
.ImagePanel 
= wxPanel(size 
= isize
, parent 
= iparent
, id = -1, name 
= 'backgroundPanel', style 
= wxSIMPLE_BORDER | wxCLIP_CHILDREN
, pos
= ipos
) 
  40         self
.clear 
= wxColour(255, 255, 255) 
  41         self
.ImagePanel
.SetBackgroundColour(self
.clear
)     # clear the panel 
  43         self
.image_sizex 
= isize
.width
 
  44         self
.image_sizey 
= isize
.height
 
  45         self
.image_posx 
= ipos
.x
 
  46         self
.image_posy 
= ipos
.y
 
  48         wxInitAllImageHandlers() 
  50     def SetValue(self
, file_nm
):    # display the selected file in the panel 
  51         image 
= ConvertBMP(file_nm
).ConvertToBitmap() 
  55         iwidth 
= image
.GetWidth()   # dimensions of image file 
  56         iheight 
= image
.GetHeight() 
  58         diffx 
= (self
.image_sizex 
- iwidth
)/2   # center calc 
  59         if iwidth 
>= self
.image_sizex 
-10:      # if image width fits in window adjust 
  61             iwidth 
= self
.image_sizex 
- 10 
  63         diffy 
= (self
.image_sizey 
- iheight
)/2  # center calc 
  64         if iheight 
>= self
.image_sizey 
- 10:    # if image height fits in window adjust 
  66             iheight 
= self
.image_sizey 
- 10 
  69         wxStaticBitmap(self
.win
, -1, image
, wxPoint(self
.image_posx
+diffx
, self
.image_posy
+diffy
), wxSize(iwidth
, iheight 
)) 
  71     def ClearPanel(self
):   # clear the image panel 
  72         self
.ImagePanel
.SetBackgroundColour(self
.clear
) 
  73         self
.ImagePanel
.Refresh() 
  75 class ImageDialog(wxDialog
): 
  76     def __init__(self
, parent
, set_dir 
= None): 
  77         wxDialog
.__init
__(self
, parent
, -1, "Image Browser", wxPyDefaultPosition
, wxSize(400, 400)) 
  79         self
.x_pos 
= 30     # initial display positions 
  85         self
.set_dir 
= os
.getcwd() 
  88             if os
.path
.exists(set_dir
):     # set to working directory if nothing set 
  89                 self
.set_dir 
= set_dir
 
  91         self
.dir_x 
= self
.x_pos
 
  92         self
.dir_y 
= self
.y_pos
 
  93         self
.DisplayDir()       # display the directory value 
  95         self
.y_pos 
= self
.y_pos 
+ self
.delta
 
  98         wxButton(self
, mID
, ' Set Directory ', wxPoint(self
.x_pos
, self
.y_pos
), size
).SetDefault() 
  99         EVT_BUTTON(self
, mID
, self
.SetDirect
) 
 101         self
.type_posy 
= self
.y_pos     
# save the y position for the image type combo         
 103         self
.fl_ext 
= '*.bmp'   # initial setting for file filtering 
 104         self
.GetFiles()     # get the file list 
 106         self
.y_pos 
= self
.y_pos 
+ self
.delta 
+ 10 
 108         self
.list_height 
= 150 
 112         self
.tb 
= tb 
= wxListBox(self
, mID
, wxPoint(self
.x_pos
, self
.y_pos
), wxSize(160, self
.list_height
), self
.fl_list
, wxLB_SINGLE
) 
 113         EVT_LISTBOX(self
, mID
, self
.OnListClick
) 
 114         EVT_LISTBOX_DCLICK(self
, mID
, self
.OnListDClick
) 
 116         width
, height 
= self
.tb
.GetSizeTuple() 
 117         image_posx 
= self
.x_pos 
+ width 
+ 20       # positions for setting the image window 
 118         image_posy 
= self
.y_pos
 
 120         image_sizey 
= self
.list_height
 
 122         self
.fl_types 
= ["Bmp", "Gif", "Png", "Jpg"] 
 123         self
.fl_ext_types 
= { "Bmp": "*.bmp", "Gif": "*.gif", "Png": "*.png", "Jpg": "*.jpg" }
 
 125         self
.set_type 
= self
.fl_types
[0]    # initial file filter setting 
 128         self
.sel_type 
= wxComboBox(self
, mID
, self
.set_type
, wxPoint(image_posx 
, self
.type_posy
), wxSize(150, -1), self
.fl_types
, wxCB_DROPDOWN
) 
 129         EVT_COMBOBOX(self
, mID
, self
.OnSetType
) 
 131         self
.image_view 
= ImageView(self
, wxPoint(image_posx
, image_posy
), wxSize(image_sizex
, image_sizey
)) 
 133         self
.y_pos 
= self
.y_pos 
+ height 
+ 20 
 136         wxButton(self
, mID
, ' Select ', wxPoint(100, self
.y_pos
), size
).SetDefault() 
 137         EVT_BUTTON(self
, mID
, self
.OnOk
) 
 139         wxButton(self
, wxID_CANCEL
, 'Cancel', wxPoint(250, self
.y_pos
), size
) 
 141         self
.y_pos 
= self
.y_pos 
+ self
.delta
 
 142         fsize 
= wxSize(400, self
.y_pos 
+ 50)    # resize dialog for final vertical position 
 147     def GetFiles(self
):     # get the file list using directory and extension values 
 148         self
.fl_val 
= FindFiles(self
, self
.set_dir
, self
.fl_ext
) 
 149         self
.fl_list 
= self
.fl_val
.files
 
 151     def DisplayDir(self
):       # display the working directory 
 152         wxStaticText(self
, -1, self
.set_dir
, wxPoint(self
.dir_x
, self
.dir_y
), wxSize(250, -1)) 
 154     def OnSetType(self
, event
): 
 155         val 
= event
.GetString()      # get file type value 
 156         self
.fl_ext 
= self
.fl_ext_types
[val
] 
 159     def OnListDClick(self
, event
): 
 162     def OnListClick(self
, event
): 
 163         val 
= event
.GetSelection() 
 164         self
.SetListValue(val
) 
 166     def SetListValue(self
, val
): 
 167         file_nm 
= self
.fl_list
[val
] 
 168         self
.set_file 
= file_val 
= os
.path
.join(self
.set_dir
, file_nm
) 
 169         self
.image_view
.SetValue(file_val
) 
 171     def SetDirect(self
, event
):     # set the new directory 
 172         dlg 
= wxDirDialog(self
) 
 173         dlg
.SetPath(self
.set_dir
) 
 174         if dlg
.ShowModal() == wxID_OK
: 
 175             self
.set_dir 
= dlg
.GetPath() 
 179     def ResetFiles(self
):   # refresh the display with files and initial image 
 180         self
.image_view
.ClearPanel() 
 183         self
.tb
.Set(self
.fl_list
) 
 185             self
.tb
.SetSelection(0) 
 193     def OnCancel(self
, event
): 
 195         self
.EndModal(wxID_CANCEL
) 
 197     def OnOk(self
, event
): 
 198         self
.result 
= self
.set_file
 
 199         self
.EndModal(wxID_OK
) 
 203     dlg 
= wxFileDialog(self
, "Choose an Image File", ".", "", "Bmp (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg", wxOPEN
) 
 204     if dlg
.ShowModal() == wxID_OK
: 
 212     def __init__(self
, parent
, dir, mask
): 
 217         mask 
= string
.upper(mask
) 
 219         for i 
in os
.listdir(dir): 
 220             if i 
== "." or i 
== "..": 
 222             path 
= os
.path
.join(dir, i
) 
 223             path 
= string
.upper(path
) 
 224             value 
= string
.upper(i
) 
 226             if self
.regex
.match(value
) == len(value
): 
 229             self
.files 
= filelist
 
 231     def MakeRegex(self
, pattern
): 
 233         f 
= ""  # Set up a regex for file names 
 244         self
.regex 
= regex
.compile(f
) 
 246     def StripExt(self
, file_nm
): 
 247         fl_fld 
= os
.path
.splitext(file_nm
)