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