Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pynxc/waxy/image.py~
blob: 7b0d062e236bb85ed4d9d0575f5b296acdb5219f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# image.py

import wx
import waxyobject

_handlers = {}

def AddImageHandler(type):
    d = {
        "bmp": wx.BMPHandler,
        "png": wx.PNGHandler,
        "jpg": wx.JPEGHandler,
        "gif": wx.GIFHandler,
        "pcx": wx.PCXHandler,
        "pnm": wx.PNMHandler,
        "tiff": wx.TIFFHandler,
        #"iff": wx.IFFHandler,
        #"xpm": wx.XPMHandler,
        "ico": wx.ICOHandler,
        "cur": wx.CURHandler,
        "ani": wx.ANIHandler,
    }
    key = type.lower()
    handler = d[key]
    wx.Image_AddHandler(handler())
    _handlers[key] = 1

def AddAllImageHandlers():
    wx.InitAllImageHandlers()

class Image(wx.Image, waxyobject.WaxyObject):

    def __init__(self, filename, type=None, autoinstall=False):
        lfilename = filename.lower()
        t = 0

        # if type isn't set, try to grok it from the filename.
        if not type:
            if lfilename.endswith(".bmp"):
                type = 'bmp'
            elif lfilename.endswith(".gif"):
                type = 'gif'
            elif lfilename.endswith(".png"):
                type = 'png'
            elif lfilename.endswith(".jpg") or lfilename.endswith(".jpeg"):
                type = 'jpg'
            elif lfilename.endswith(".pcx"):
                type = 'pcx'
            elif lfilename.endswith(".ico"):
                type = 'ico'

        t = {
            "bmp": wx.BITMAP_TYPE_BMP,
            "gif": wx.BITMAP_TYPE_GIF,
            "png": wx.BITMAP_TYPE_PNG,
            "jpg": wx.BITMAP_TYPE_JPEG,
            "jpeg": wx.BITMAP_TYPE_JPEG,
            "pcx": wx.BITMAP_TYPE_PCX,
            "ico": wx.BITMAP_TYPE_ICO,
        }.get(type.lower(), 0)

        if not t:
            raise ValueError, "Could not determine bitmap type of '%s'" % (
                  filename,)

        # if autoinstall is true, install handler on demand
        if autoinstall:
            if not _handlers.has_key(type):
                AddImageHandler(type)

        wx.Image.__init__(self, filename, t)


def ImageAsBitmap(filename, *args, **kwargs):
    return Image(filename, *args, **kwargs).ConvertToBitmap()

class ImagePanel(wx.Panel, waxyobject.WaxyObject):

    def __init__(self,parent,image_filename,size=(200,200),pos=(0,0)):
    
        wx.Panel.__init__(self,parent, size=size,pos=pos)
       
        if isinstance(image_filename,str):
        
            self.image=Image(image_filename)
        else:
            self.image=image_filename
        
        self.bitmap=wx.StaticBitmap(self, -1, self.image.ConvertToBitmap(),
                                    style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.update()

    def SetImage(self,image):
        
        if isinstance(image,str):
            image=Image(image)
            
        self.image=image
        self.update()

    def update(self):
    
        size=self.GetSizeTuple()
        
        imsize=(self.image.GetWidth(),self.image.GetHeight())
        
        if (float(imsize[0])/float(size[0]))>(float(imsize[1])/float(size[1])):
            scale=(float(imsize[0])/float(size[0]))
        else:
            scale=(float(imsize[1])/float(size[1]))
            
        newsize=(int(imsize[0]/scale),int(imsize[1]/scale))
        newpos=( (size[0]-newsize[0])/2, (size[1]-newsize[1])/2)
        self.bitmap.SetBitmap(self.image.Rescale(newsize[0],newsize[1]).ConvertToBitmap())
        self.bitmap.SetPosition(newpos);