How to resize and draw an image using wxpython?

aF. picture aF. · Mar 23, 2010 · Viewed 25k times · Source

I want to load an image, resize it to a given size and after draw it in a specific position in a panel.

All this using wxpython.

How can I do it?

Thanks in advance!

Answer

FogleBird picture FogleBird · Mar 24, 2010

wx.Image has a Scale method that will do the resizing. The rest is normal wx coding.

Here's a complete example for you.

import wx

def scale_bitmap(bitmap, width, height):
    image = wx.ImageFromBitmap(bitmap)
    image = image.Scale(width, height, wx.IMAGE_QUALITY_HIGH)
    result = wx.BitmapFromImage(image)
    return result

class Panel(wx.Panel):
    def __init__(self, parent, path):
        super(Panel, self).__init__(parent, -1)
        bitmap = wx.Bitmap(path)
        bitmap = scale_bitmap(bitmap, 300, 200)
        control = wx.StaticBitmap(self, -1, bitmap)
        control.SetPosition((10, 10))

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = wx.Frame(None, -1, 'Scaled Image')
    panel = Panel(frame, 'input.jpg')
    frame.Show()
    app.MainLoop()