doc: Add Convolution examples to image generator.
[gfxprim/pasky.git] / doc / images / regen.py
blob3e01756f1983635173d8260cf5becf8a64a05ef2
1 #!/usr/bin/env python
3 import sys
5 import gfxprim.core as core
6 import gfxprim.loaders as loaders
7 import gfxprim.filters as filters
9 def str_esc(x):
10 res = str(x)
11 if (res[0] == '-'):
12 res = '\\' + res
13 return res
15 def to_str(x, to_str):
16 res = ''
17 if hasattr(x, '__iter__'):
18 for i in x:
19 if hasattr(i, '__iter__'):
20 res += ('(')
21 first = 1
22 for j in i:
23 if (first):
24 first = 0
25 else:
26 res += ','
27 res += to_str(j)
28 res += (')')
29 else:
30 res += ('(' + to_str(i)+ ')')
31 else:
32 return to_str(x)
34 return res
36 class ImgGen:
37 def __init__(self, orig_path):
38 self.orig_path = orig_path
39 self.img = loaders.Load('../' + orig_path + 'lenna.png')
40 self.img_small = loaders.Load('../' + orig_path + 'lenna_small.png')
42 def write_asciidoc_head(self, dst_path, heading):
43 self.f = open('../' + dst_path + 'images.txt', 'w')
44 f = self.f
45 f.write('.Original Image; ' + heading + '\n')
46 f.write('image:' + self.orig_path + 'lenna_small.png[\n')
47 f.write('\t"Original Image",\n')
48 f.write('\tlink="' + self.orig_path + 'lenna.png"]\n')
50 def write_img_asciidoc(self, desc, fname, fname_small):
51 f = self.f
52 f.write('image:' + fname_small + '[\n')
53 f.write('\t"' + desc + '",\n')
54 f.write('\tlink="' + fname + '"]\n')
56 def write_asciidoc_tail(self):
57 self.f.write('\n')
58 self.f.close()
60 def gen(self, func, func_param_desc, func_params_arr, dst_path, func_name, descs=None):
62 print("Generating " + func_name)
64 head = func_name + ' '
65 head += ', '.join(map(lambda x: ' '.join([func_param_desc[i] + '=' +
66 str(x[i]) for i in range(0, len(x))]), func_params_arr))
68 if (descs is not None):
69 head = ', '.join(descs)
71 self.write_asciidoc_head(dst_path, head)
73 for i in range(0, len(func_params_arr)):
74 params = func_params_arr[i]
75 str_pars = [to_str(x, str) for x in params]
77 if (descs is not None):
78 desc = descs[i]
79 else:
80 desc = func_name + ' ' + ' '.join(str_pars)
82 print(' > ' + desc)
84 fname = dst_path + 'lenna_' + ','.join(str_pars) + '.png'
85 fname_small = dst_path + 'lenna_small_' + ','.join(str_pars) + '.png'
87 self.write_img_asciidoc(desc, fname, fname_small)
89 res = func(self.img, *params)
90 res.loaders.Save('../' + fname)
92 res = func(self.img_small, *params)
93 res.loaders.Save('../' + fname_small)
95 self.write_asciidoc_tail()
97 def main():
98 imggen = ImgGen('images/orig/')
100 imggen.gen(filters.InvertAlloc, [],
101 [[]],
102 'images/invert/', 'Inverted')
104 imggen.gen(filters.BrightnessAlloc, ['p'],
105 [[-.5], [-.2], [.2], [.5]],
106 'images/brightness/', 'Brightness')
108 imggen.gen(filters.ContrastAlloc, ['p'],
109 [[.2], [.5], [1.5], [2], [3]],
110 'images/contrast/', 'Contrast')
112 imggen.gen(filters.BrightnessContrastAlloc, ['b', 'c'],
113 [[-.2, .8], [-.5, 2], [.2, .8], [.2, 1.5]],
114 'images/brightness_contrast/', 'BrightnessContrast')
116 imggen.gen(filters.PosterizeAlloc, ['s'],
117 [[2], [3], [4], [5], [6]],
118 'images/posterize/', 'Posterize')
120 imggen.gen(filters.MirrorHAlloc, [],
121 [[]],
122 'images/mirror_h/', 'Mirrored Horizontally')
124 imggen.gen(filters.MirrorVAlloc, [],
125 [[]],
126 'images/mirror_v/', 'Mirrored Vertically')
128 imggen.gen(filters.Rotate90Alloc, [],
129 [[]],
130 'images/rotate_90/', 'Rotated by 90 degrees')
132 imggen.gen(filters.Rotate180Alloc, [],
133 [[]],
134 'images/rotate_180/', 'Rotated by 180 degrees')
136 imggen.gen(filters.Rotate270Alloc, [],
137 [[]],
138 'images/rotate_270/', 'Rotated by 270 degrees')
140 imggen.gen(filters.GaussianBlurAlloc, ['xsig', 'ysig'],
141 [[2, 2], [0, 4], [4, 0], [4, 4], [10, 10]],
142 'images/blur/', 'Gaussian Blur')
144 imggen.gen(filters.MedianAlloc, ['xr', 'yr'],
145 [[3, 3], [5, 5], [7, 7], [9, 9], [12, 12]],
146 'images/median/', 'Median')
148 imggen.gen(filters.EdgeSharpeningAlloc, ['w'],
149 [[0.1], [0.3], [0.5], [0.8], [1.0]],
150 'images/edge_sharpening/', 'Edge Sharpening')
152 imggen.gen(filters.GaussianNoiseAddAlloc, ['s', 'm'],
153 [[0.03, 0], [0.05, 0], [0.05, -0.1], [0.05, 0.1], [0.07, 0.0]],
154 'images/gaussian_noise/', 'Gaussian Additive Noise')
156 imggen.gen(filters.ConvolutionAlloc, ['k', 'kd'],
158 [[[1, 1, 1],
159 [1, 1, 1],
160 [1, 1, 1]], 9],
161 [[[1, 1, 1, 1, 1],
162 [1, 1, 1, 1, 1],
163 [1, 1, 1, 1, 1],
164 [1, 1, 1, 1, 1],
165 [1, 1, 1, 1, 1]], 25],
166 [[[ 0, -1, 0],
167 [-1, 4, -1],
168 [ 0, -1, 0]], 1],
169 [[[1, 0, -1],
170 [2, 0, -2],
171 [1, 0, -1]], 1],
172 [[[0, 0, 0],
173 [1, -1, 0],
174 [0, 0, 0]], 1],
176 'images/convolution/', 'Convolution',
177 ['3x3 Box Blur',
178 '5x5 Box Blur',
179 '3x3 Laplacian',
180 '3x3 Sobel',
181 '3x3 Roberts'])
183 if __name__ == '__main__':
184 main()