Created
February 12, 2021 20:41
-
-
Save driscollis/eb7aae8d3f80a93f08e463c8d6311119 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# image_viewer.py | |
import io | |
import wx | |
class ImagePanel(wx.Panel): | |
def __init__(self, parent, image_size): | |
super().__init__(parent) | |
self.max_size = 240 | |
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) | |
self.photo_txt = wx.TextCtrl(self, size=(200, -1)) | |
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) | |
hsizer.Add(self.photo_txt, 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): | |
""" | |
Browse for an image file | |
@param event: The event object | |
""" | |
wildcard = "JPEG files (*.jpg)|*.jpg" | |
with wx.FileDialog( | |
None, "Choose a file", wildcard=wildcard, | |
style=wx.FD_OPEN | |
) as dialog: | |
if dialog.ShowModal() == wx.ID_OK: | |
self.photo_txt.SetValue(dialog.GetPath()) | |
self.load_image() | |
def load_image(self): | |
""" | |
Load the image and display it to the user | |
""" | |
filepath = self.photo_txt.GetValue() | |
with open(filepath, "rb") as f: | |
data = f.read() | |
buff = io.BytesIO(data) | |
img = wx.Image(buff) | |
# scale the image, preserving the aspect ratio | |
W = img.GetWidth() | |
H = img.GetHeight() | |
if W > H: | |
new_w = self.max_size | |
new_h = self.max_size * H / W | |
else: | |
new_h = self.max_size | |
new_w = self.max_size * W / H | |
img = img.Scale(int(new_w), int(new_h)) | |
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