~bkrpr/bookliberator/trunk

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
117
118
119
120
121
#!/usr/bin/python

import wx
import copy

class BkImage:
    def __init__(self, seed=None):
        if isinstance(seed, str) or isinstance(seed, unicode):
            self.im = wx.Image(seed)
            self.path = seed
        elif hasattr(seed, 'path'):
            pass
        elif isinstance(seed, wx.Image):
            self.im = seed
            seed.path = None
        else:
            assert False, "Unhandled type in image init: %s" % seed.__class__.__name__

        self.setsize()

    def GetSize(self):
        return self.im.GetSize()

    def setsize(self):
        self.w, self.h = self.im.GetSize()
        self.size = self.im.GetSize()
        
    def save(self, name, type):
        self.im.SaveFile(name, type)

    def Thumbnail(self, width, height=None):
        '''Returns scaled image so it is as big as possible but
        neither dimension is greater than the width and height
        specified as params.

        Returns scaled image '''
        
        try:
            width, height = width[0], width[1]
        except TypeError:
            pass

        if not width or not height:
            return self

        w,h = self.size

        assert h, "Height should not be zero!"

        aspect = float(w)/h

        if width/aspect <= height:
            im = self.im.Scale(width, int(width/aspect))
        elif aspect*height <= width:
            im = self.im.Scale(int(aspect*height), height)
        else:
            assert False, "Control should never reach here."
        
        return BkImage(im)

    def wxBitmap(self):
        '''Returns a wx.Bitmap of self.'''
        return wx.BitmapFromImage(self.im)

    def Crop(self, x,y, a=None, b=None):
        '''Crop using (x,y), (a,b) as start and end points.

        If x and y are 2-tuples, x is start and y is end point.

        Returns self for convenience.'''

        if x == None or y == None:
            return self

        try:
            a,b = y[0], y[1]
            y = x[1]
            x = x[0]
        except TypeError:
            pass

        size = wx.Size(abs(x-a), abs(y-b))
        pos = wx.Point(-x,-y)
        self.im.Resize(size,pos, 0,0,0)
        self.setsize()
        return self

    def Rotate(self, degrees):
        '''Returns a copy of self rotated degrees degrees, where
        degrees is a multiple of 90.'''

        if degrees == None:
            return self

        degrees = int(degrees) % 360

        if not degrees:
            return self

        assert degrees in [90,180,270], "degrees must be a multiple of 90 (not %s)." % degrees

        f = {90:self.Rotate90, 180:self.Rotate180, 270:self.Rotate270}[degrees]
        return f()

    def Rotate90(self):
        '''Returns a copy of self rotated 90 degrees.'''
        return BkImage(self.im.Rotate90(False))

    def Rotate180(self):
        '''Returns a copy of self rotated 180 degrees.'''
        return self.Rotate90().Rotate90()

    def Rotate270(self):
        '''Returns a copy of self rotated 270 degrees.'''
        return BkImage(self.im.Rotate90(True))


#if __name__ == '__main__':
#    im = BkImage("Cam2/10a.JPG")
#    i = im.Rotate90()
#    print i.GetSize()