Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize and draw an image using wxpython?

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!

like image 476
aF. Avatar asked Mar 23 '10 22:03

aF.


People also ask

How do you resize a bitmap image in Python?

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.

How do I resize and save an image in Python?

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.


1 Answers

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()
like image 126
FogleBird Avatar answered Sep 18 '22 05:09

FogleBird