Mercurial > ift6266
comparison transformations/slant.py @ 34:e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
author | ychherawala@smets-w04 |
---|---|
date | Tue, 02 Feb 2010 10:00:03 -0500 |
parents | |
children | e05715e4554d |
comparison
equal
deleted
inserted
replaced
33:6d432a5010a2 | 34:e61a46996db6 |
---|---|
1 #!/usr/bin/python | |
2 # coding: utf-8 | |
3 | |
4 ''' | |
5 this module add a slant effect to the image. | |
6 | |
7 To obtain the slant effect, each row of the array is shifted proportionately by a step | |
8 controlled by the complexity. | |
9 | |
10 ''' | |
11 | |
12 import numpy | |
13 | |
14 | |
15 class Slant(): | |
16 def __init__(self, complexity=1): | |
17 #---------- private attributes | |
18 self.direction = 1 | |
19 self.angle = 0 | |
20 | |
21 #---------- generation parameters | |
22 self.regenerate_parameters(complexity) | |
23 #------------------------------------------------ | |
24 | |
25 def _get_current_parameters(self): | |
26 return [self.angle, self.direction] | |
27 | |
28 def get_settings_names(self): | |
29 return ['angle', 'direction'] | |
30 | |
31 def regenerate_parameters(self, complexity): | |
32 self.angle = numpy.random.uniform(0.0, complexity) | |
33 P = numpy.random.uniform() | |
34 self.direction = 1; | |
35 if P < 0.5: | |
36 self.direction = -1; | |
37 return self._get_current_parameters() | |
38 | |
39 | |
40 def transform_image(self,image): | |
41 if self.angle == 0: | |
42 return image | |
43 | |
44 ysize, xsize = image.shape | |
45 slant = self.direction*self.angle | |
46 | |
47 output = image.copy() | |
48 | |
49 # shift all the rows | |
50 for i in range(ysize): | |
51 line = image[i] | |
52 delta = round((i*slant)) % xsize | |
53 line1 = line[:xsize-delta] | |
54 line2 = line[xsize-delta:xsize] | |
55 | |
56 output[i][delta:xsize] = line1 | |
57 output[i][0:delta] = line2 | |
58 | |
59 | |
60 #correction to center the image | |
61 correction = (self.direction)*round(self.angle*ysize/2) | |
62 correction = (xsize - correction) % xsize | |
63 | |
64 # center the region | |
65 line1 = output[0:ysize,0:xsize-correction].copy() | |
66 line2 = output[0:ysize,xsize-correction:xsize].copy() | |
67 output[0:ysize,correction:xsize] = line1 | |
68 output[0:ysize,0:correction] = line2 | |
69 | |
70 | |
71 return output | |
72 | |
73 | |
74 # Test function | |
75 def test_slant(): | |
76 img_name = "3.png" | |
77 dest_img_name = "slanted.png" | |
78 im = Image.open(img_name,) | |
79 im = im.convert("L") | |
80 image = numpy.asarray(im) | |
81 | |
82 | |
83 slant = Slant() | |
84 slant.regenerate_parameters(1) | |
85 image = slant.transform_image(image) | |
86 | |
87 im = Image.fromarray(image.astype('uint8'), "L") | |
88 im.save(dest_img_name) | |
89 | |
90 # Test | |
91 if __name__ == '__main__': | |
92 import sys, os, fnmatch | |
93 import Image | |
94 | |
95 test_slant() | |
96 |