Created
May 21, 2020 18:13
-
-
Save driscollis/6e6cceddc26c5a8f2a56bcf475f8b3b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wx | |
class ImagePanel(wx.Panel): | |
def __init__(self, parent, image_size): | |
super().__init__(parent) | |
self.max_size = 240 | |
self.file_path = None | |
img = wx.Image(*image_size) | |
self.image_ctrl = wx.StaticBitmap(self, bitmap=wx.Bitmap(img)) | |
browse_btn = wx.Button(self, label='Browse') | |
browse_btn.Bind(wx.EVT_BUTTON, self.on_browse) | |
main_sizer = wx.BoxSizer(wx.VERTICAL) | |
hsizer = wx.BoxSizer(wx.HORIZONTAL) | |
main_sizer.Add(self.image_ctrl, 0, wx.ALL, 5) | |
hsizer.Add(browse_btn, 0, wx.ALL, 5) | |
main_sizer.Add(hsizer, 0, wx.ALL, 5) | |
self.SetSizer(main_sizer) | |
main_sizer.Fit(parent) | |
self.Layout() | |
def on_browse(self, event): | |
wildcard = "JPEG files (*.jpg)|*.jpg" | |
with wx.FileDialog(None, "Choose a file", | |
wildcard=wildcard, | |
style=wx.ID_OPEN) as dialog: | |
if dialog.ShowModal() == wx.ID_OK: | |
self.file_path = dialog.GetPath() | |
self.load_image() | |
def load_image(self): | |
if self.file_path is None: | |
return | |
img = wx.Image(self.file_path, wx.BITMAP_TYPE_ANY) | |
W = img.GetWidth() | |
H = img.GetHeight() | |
if W > H: | |
NewW = self.max_size | |
NewH = self.max_size * H / W | |
else: | |
NewH = self.max_size | |
NewW = self.max_size * W / H | |
img = img.Scale(NewW, NewH) | |
self.image_ctrl.SetBitmap(wx.Bitmap(img)) | |
self.Refresh() | |
class MainFrame(wx.Frame): | |
def __init__(self): | |
super().__init__(None, title='Image Viewer') | |
panel = ImagePanel(self, image_size=(240, 240)) | |
self.Show() | |
if __name__ == '__main__': | |
app = wx.App(redirect=False) | |
frame = MainFrame() | |
app.MainLoop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment