How to resize and draw an image using wxpython?

前端 未结 3 660
感动是毒
感动是毒 2020-12-29 11:01

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?

Tha

3条回答
  •  情书的邮戳
    2020-12-29 11:28

    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()
    

提交回复
热议问题