PIL und wxPython

Aus Physik
Wechseln zu: Navigation, Suche

Dieses Beispiel zeigt, wie man die Python Imaging Library zusammen mit wxPython verwenden kann, um ein bmp Bild zu transformieren, als png Bild abzuspeichern und anschließend in einem Fenster graphisch darzustellen.

from wxPython.wx import *
import Image

fileopen="desktop_07_m_1280x960.bmp"
filesave="desktopconverted.png"

class TestFrame(wxFrame):
    def __init__(self, parent,filename):
        self.filename=filename
        wxFrame.__init__(self, parent, -1,"Test", size = (800, 600))
        window=wxScrolledWindow(self, -1, wxPoint(0, 0), wxDefaultSize, wxSUNKEN_BORDER)
        png = wxImage(self.filename, wxBITMAP_TYPE_PNG).ConvertToBitmap()
        wxStaticBitmap(window, -1, png, wxPoint(0, 0),
                   wxSize(png.GetWidth(), png.GetHeight()))
        window.SetScrollbars(1,1,png.GetWidth(),png.GetHeight())
        self.Centre(wxBOTH)
        
class TestApp(wxApp):
    def OnInit(self):
        wxInitAllImageHandlers()
        wxToolTip_Enable(true)
        try: self.SetAssertMode(wxPYAPP_ASSERT_SUPPRESS)
        except AttributeError: pass # < 2.3.4
        wxYield()
        return true
    def Show(self,filename):
        self.frame = TestFrame(None,filename)
        self.frame.SetSize(wxSize(400,400))
        self.frame.Show(true)
        self.SetTopWindow(self.frame)
        self.frame2 = TestFrame(None,filename)
        self.frame2.SetSize(wxSize(200,200))
        self.frame2.Show(true)
            


def main():
        im=Image.open(fileopen).convert("RGB")        
        im2=im.resize((800,300),Image.ANTIALIAS)
        im2.save(filesave,"PNG")
        app =  TestApp(0)
        app.Show(filesave);
        app.MainLoop()
        
if __name__ == '__main__':
    main()