source: src/django_gheat/gheat/pil_.py@ 9750

Last change on this file since 9750 was 9006, checked in by dennisw, 14 years ago

django_gheat - versie van gheat die gebruik maakt van django framework, ondersteunt SQL connecties in tegenstelling tot gheat dat gebruik maakt van sqlite

File size: 3.1 KB
Line 
1import os
2
3from PIL import Image, ImageChops
4from gheat import SIZE, base
5from gheat.opacity import OPAQUE
6
7
8class ColorScheme(base.ColorScheme):
9
10 def hook_set(self, fspath):
11 self.colors = Image.open(fspath).load()
12
13 def hook_build_empty(self, opacity, fspath):
14 color = self.colors[0, 255]
15 if len(color) == 4: # color map has per-pixel alpha
16 (conf, pixel) = opacity, color[3]
17 opacity = int(( (conf/255.0) # from configuration
18 * (pixel/255.0) # from per-pixel alpha
19 ) * 255)
20 color = color[:3] + (opacity,)
21 tile = Image.new('RGBA', (SIZE, SIZE), color)
22 tile.save(fspath, 'PNG')
23
24
25class Dot(base.Dot):
26 def hook_get(self, fspath):
27 img = Image.open(fspath)
28 half_size = img.size[0] / 2
29 return img, half_size
30
31
32class Tile(base.Tile):
33 """Represent a tile; use the PIL backend.
34 """
35
36 def hook_rebuild(self, points):
37 """Given a list of points and an opacity, save a tile.
38
39 This uses the PIL backend.
40
41 """
42 tile = self._start()
43 tile = self._add_points(tile, points)
44 tile = self._trim(tile)
45 foo = self._colorize(tile) # returns None
46 return tile
47
48
49 def _start(self):
50 return Image.new('RGBA', self.expanded_size, 'white')
51
52
53 def _add_points(self, tile, points):
54 for x,y in points:
55 dot_placed = Image.new('RGBA', self.expanded_size, 'white')
56 dot_placed.paste(self.dot, (x, y))
57 tile = ImageChops.multiply(tile, dot_placed)
58 return tile
59
60
61 def _trim(self, tile):
62 tile = tile.crop((self.pad, self.pad, SIZE+self.pad, SIZE+self.pad))
63 tile = ImageChops.duplicate(tile) # converts ImageCrop => Image
64 return tile
65
66
67 def _colorize(self, tile):
68 _computed_opacities = dict()
69 pix = tile.load() # Image => PixelAccess
70 for x in range(SIZE):
71 for y in range(SIZE):
72
73 # Get color for this intensity
74 # ============================
75 # is a value
76
77 val = self.color_scheme.colors[0, pix[x,y][0]]
78 try:
79 pix_alpha = val[3] # the color image has transparency
80 except IndexError:
81 pix_alpha = OPAQUE # it doesn't
82
83
84 # Blend the opacities
85 # ===================
86
87 conf, pixel = self.opacity, pix_alpha
88 if (conf, pixel) not in _computed_opacities:
89 opacity = int(( (conf/255.0) # from configuration
90 * (pixel/255.0) # from per-pixel alpha
91 ) * 255)
92 _computed_opacities[(conf, pixel)] = opacity
93
94 pix[x,y] = val[:3] + (_computed_opacities[(conf, pixel)],)
95
96
97 def save(self):
98 self.img.save(self.fspath, 'PNG')
99
100
Note: See TracBrowser for help on using the repository browser.