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!
To resize an image, you call the resize() method on it, passing in a two-integer tuple argument representing the width and height of the resized image. The function doesn't modify the used image; it instead returns another Image with the new dimensions.
Practical Data Science using Python The Image module from pillow library has an attribute size. This tuple consists of width and height of the image as its elements. To resize an image, you call the resize() method of pillow's image class by giving width and height.
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()
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