My wxPython project has a frame, with multiple nested sizers.
One of the sizers contains a wxStaticImage with a bitmap that is read from a file.
I need the image to resize (grow/shrink) every time the frame is resized, so it will fit it's sizer's boundaries.
(I think that) I know how to resize an image. What I don't know is how to:
(for now, I don't mind the proportions)
Edit: Complete solution
i understood wrong about wxStaticBitmapin.Size. it does NOT describe the size of the image (i.e. image resolution), but rather - wxStaticBitmapin.Size gives the sizer's slot dimentions, or in other words: the current widget's size.
so with Mik's code i now how to fit into the slot.
in addition to mike's solution: when using onSize event on a frame, don't forget to add event.skip(). otherwise the sizers will stop re-aligning. Altertanively, just use the image's onSize.
here's the complete event method:
def bitmap1_onSize(self, e=None):
W, H = self.bitmap1.Size
if W > H:
NewW = W
NewH = W * H / W
else:
NewH = H
NewW = H * W / H
img = wx.Image(self.frame_file_picker.Path, wx.BITMAP_TYPE_ANY)
img = img.Scale(NewW,NewH)
self.bitmap1.SetBitmap(wx.BitmapFromImage(img))
e.Skip()
You will need to catch EVT_SIZE or EVT_SIZING. You can check out this tutorial I wrote about creating an image viewer. It has some scaling code in it: http://www.blog.pythonlibrary.org/2010/03/26/creating-a-simple-photo-viewer-with-wxpython/
I would just take that scaling code and use it to update your image. You'll want to make sure you stop scaling your image up past its maximum size or you'll end up with a lot of pixelization though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With