annotate transformations/slant.py @ 35:e05715e4554d

first commit, the module has been tested with testmod.py, it has no timing
author youssouf
date Tue, 02 Feb 2010 10:11:33 -0500
parents e61a46996db6
children 0f1337994716
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 '''
35
e05715e4554d first commit, the module has been tested with testmod.py, it has no timing
youssouf
parents: 34
diff changeset
5 Author: Youssouf
e05715e4554d first commit, the module has been tested with testmod.py, it has no timing
youssouf
parents: 34
diff changeset
6
34
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
7 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
8
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
9 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
10 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
11
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
12 '''
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 import numpy
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
15
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
16
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
17 class Slant():
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
18 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
19 #---------- private attributes
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
20 self.direction = 1
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
21 self.angle = 0
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
22
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
23 #---------- generation parameters
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
24 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
25 #------------------------------------------------
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
26
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
27 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
28 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
29
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
30 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
31 return ['angle', 'direction']
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
32
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
33 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
34 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
35 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
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 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
38 self.direction = -1;
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
39 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
40
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
41
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
42 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
43 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
44 return image
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
45
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
46 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
47 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
48
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
49 output = image.copy()
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
50
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
51 # 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
52 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
53 line = image[i]
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
54 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
55 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
56 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
57
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
58 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
59 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
60
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
61
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
62 #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
63 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
64 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
65
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
66 # center the region
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
67 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
68 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
69 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
70 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
71
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 return output
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
74
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
75
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
76 # Test function
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
77 def test_slant():
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
78 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
79 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
80 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
81 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
82 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
83
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
84
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
85 slant = Slant()
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
86 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
87 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
88
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
89 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
90 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
91
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
92 # Test
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
93 if __name__ == '__main__':
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
94 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
95 import Image
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
96
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
97 test_slant()
e61a46996db6 first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff changeset
98